C# byte array to image not working with identical byte arrays - c#

The complete code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Display byte array as image
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Image img = ByteArrayToImage(File.ReadAllBytes("")); //fill with path info
pictureBox1.Image = (Image)img.Clone();
}
//Convert to image from bytes
public Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
//Open Image for conversion
private void loadImage_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
DialogResult dr = new DialogResult();
dr = opf.ShowDialog();
if (dr == DialogResult.OK)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(opf.FileName);
}
}
private void convertImage_Click(object sender, EventArgs e)
{
//Choose Path to Save To
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Title = "Choose Directory and File Name";
saveDialog.Filter = "Canga Comix *.CCMX|*.CCMX";
DialogResult dr = saveDialog.ShowDialog();
if (dr == DialogResult.OK)
{
byte[] array;
//Save Image
using (MemoryStream ms = new MemoryStream())
{
Image img = pictureBox1.Image;
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
array = ms.ToArray();
}
using(FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create))
{
fs.Write(array, 0, array.Length);
}
}
}
//clear image
private void clearImage_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
}
}
}
These are pretty standard. I have a test program that uses these methods. It opens an image in a pictureBox and then converts it to byte[] and clears the pictureBox Image. Then you can hit Load byte[] and it will display the picture properly as it runs the ByteArrayToImage method.
Then if I save out a picture from my other program and try to open it in the test program it gives me the unholy "Parameter is not valid" error. Even though both text documents are exactly the same as far as I can tell.

This code contains a common problem. Here you create an Image bound to a stream containing your bitmap...
public Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
ms.Position = 0;
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
... and the using block disposes the MemoryStream instance on the way out, which makes the Image rather useless.
You can get an Image that manages its own memory instead of expecting your stream to stick around by calling Clone():
public Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
using (Image returnImage = Image.FromStream(ms))
{
return returnImage.Clone();
}
}
The clone isn't bound to the original stream.

Related

C# how to use variable outside of block of code

I have the following code. I want to use pictureBox1.Image = image outside of the code block like so:
private void button1_Click(object sender, EventArgs e) {
using(MemoryStream memoryStream = new MemoryStream()) {
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
}
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
}
I get the folloowing error: The name 'memoryStream' does not exist in the current context. I know I can do the following move the last 2 lines into the brackets and the code works but How can I use variable outside of the { } block of code
I know the code below works but i just want to know if there is a way to use pictureBox1.Image = image outside of the block of code.
private void button1_Click(object sender, EventArgs e)
{
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
}
}
You can't and shouldn't use the stream outside of the using scope, after that it will get disposed and you don't run into leaking issues.
You could just assign the value after the scope, when you declare the variable before it.
private void button1_Click(object sender, EventArgs e)
{
Image image;
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
// byte[] imageBytes = memoryStream.ToArray(); //unused?
image = Image.FromStream(memoryStream);
}
pictureBox1.Image = image;
}
Or better yet make a function to read the image and return it.
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = readImage();
}
private Image readImage()
{
using(MemoryStream memoryStream = new MemoryStream())
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
// byte[] imageBytes = memoryStream.ToArray(); //unused?
return Image.FromStream(memoryStream);
}
}
There's no problem putting that line outside the using block. The problem is line before. You can't use memoryStream outside the using block. The whole point of the using block is to create a scope for that variable and ensure that the object assigned to it is disposed at the end of the block. You can do this:
private void button1_Click(object sender, EventArgs e) {
Image image;
using(MemoryStream memoryStream = new MemoryStream()) {
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
image = Image.FromStream(memoryStream);
}
pictureBox1.Image = image;
}
You can define MemoryStream outside the code block:
MemoryStream memoryStream = new MemoryStream();
using (memoryStream)
{
pic.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = memoryStream.ToArray();
}
Image image = Image.FromStream(memoryStream);
pictureBox1.Image = image;
Edit:(just for test)
If you think using releases all resources for the variable defined outside, test the following code:
MemoryStream memoryStream = new MemoryStream();
using (memoryStream)
{
byte[] messageBytes = Encoding.ASCII.GetBytes("test Massage");
memoryStream.Write(messageBytes, 0, messageBytes.Length);
}
var data = Encoding.Default.GetString(memoryStream.ToArray()); // data = "test Massage"

Word Interop - Save embedded shape as image

I am attempting to save an embedded shape as an image using C#.
If the object is embedded as an actual image (WMF/JPEG) I can retrieve the image without issue but when the object is an embedded shape or an OLE Object that displays as an image in Word I cannot seem to extract or retrieve said object to then either copy to the clipboard or save said image.
Here is my current code sample; either the object is empty or I get the following error:
System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'
Any help is appreciated. Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace ImageMagickSandboxWinForms
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
public static BitmapSource ConvertBitmap(Bitmap source)
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
source.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
Bitmap bitmap;
using (var outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
string physicsDocLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
physicsDocLocation += #"\[Doc path Here].docx";
var wordApp = new Microsoft.Office.Interop.Word.Application();
var wordDoc = wordApp.Documents.Open(physicsDocLocation);
int iCount = wordDoc.InlineShapes.Count;
for (int i = 1; i < (wordDoc.InlineShapes.Count + 1); i++)
{
var currentInlineShape = wordDoc.InlineShapes[i];
currentInlineShape.Range.Select();
wordDoc.ActiveWindow.Selection.Range.Copy();
BitmapSource clipBoardImage = System.Windows.Clipboard.GetImage();
Bitmap bmpClipImage = BitmapFromSource(clipBoardImage);
string finalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), #"TestConversions");
finalPath += #"\" + Guid.NewGuid().ToString() + ".jpg";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(finalPath, FileMode.Create, FileAccess.ReadWrite))
{
bmpClipImage.Save(memory, ImageFormat.Jpeg); <<<---- Error happens here.
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
wordDoc.Close();
wordApp.Quit();
}
}
}
i have these code in my library, dunno where i have found that but hope you do the job for you: i am using Clippboard to trap the different images, jus t dont forget, Thread is needed to access Clipboard
for (var i = 1; i <= wordApplication.ActiveDocument.InlineShapes.Count; i++)
{
var inlineShapeId = i;
var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));
// STA is needed in order to access the clipboard
// https://stackoverflow.com/a/518724/700926
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
// General idea is based on: https://stackoverflow.com/a/7937590/700926
protected static void SaveInlineShapeToFile(int inlineShapeId, Application wordApplication)
{
// Get the shape, select, and copy it to the clipboard
var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];
inlineShape.Select();
wordApplication.Selection.Copy();
// Check data is in the clipboard
if (Clipboard.GetDataObject() != null)
{
var data = Clipboard.GetDataObject();
// Check if the data conforms to a bitmap format
if (data != null && data.GetDataPresent(DataFormats.Bitmap))
{
// Fetch the image and convert it to a Bitmap
var image = (Image) data.GetData(DataFormats.Bitmap, true);
var currentBitmap = new Bitmap(image);
// Save the bitmap to a file
currentBitmap.Save(#"C:\Users\Username\Documents\" + String.Format("img_{0}.png", inlineShapeId));
}
}
}
following if you are using Winform or WPF the clipboard acts differently for an image:
if (Clipboard.ContainsImage())
{
// ImageUIElement.Source = Clipboard.GetImage(); // does not work
System.Windows.Forms.IDataObject clipboardData = System.Windows.Forms.Clipboard.GetDataObject();
if (clipboardData != null)
{
if (clipboardData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
{
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)clipboardData.GetData(System.Windows.Forms.DataFormats.Bitmap);
ImageUIElement.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
Console.WriteLine("Clipboard copied to UIElement");
}
}
}
after if its not functionam due to a bug in translation of format, there is this solution . So its infrecnh but its easily to understand the logic of the using of "DeviceIndependentBitmap"

How can I add an image in my database sqlite using dbContext WPF c#?

I want to add an image to the database in an image column in byte form
I am using SQLite to save my database data and WPF Application using dbContext c# to write my code
can anyone help me please?
private void ChooseImageButtonClick(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif";
if (dlg.ShowDialog() == true)
{
string FileName = dlg.FileName.ToString();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(FileName);
bitmap.EndInit();
ImageBox.Source = bitmap;
}
}
private void savebtnClick(object sender, RoutedEventArgs e)
{
using (DatabaseContext dbContext = new DatabaseContext())
{
Person p = new Person
{
Id = int.Parse(Idtextbox.Text),
Name = Nametextbox.Text,
Image = image
};
dbContext.Person.Add(p);
dbContext.SaveChanges();
RefreshList();
}
}
Just convert BitmapImage to byte array first
byte[] image;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 100;
using (MemoryStream ms = new MemoryStream())
{
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)ImageBox.Source));
encoder.Save(ms);
image = ms.ToArray();
}
encoder = null;
And in your database context - add
using (DatabaseContext dbContext = new DatabaseContext())
{
Part part = new Part();
part.Id = int.Parse(TextBoxID.Text);
part.Name = TextBoxName.Text;
part.Image = image;
dbContext.Part.Add(part);
dbContext.SaveChanges();
RefreshPartsList();
}}
To convert byte array back to BitmapImage :
byte[] imageData = part.Image; // that you get from db
if (imageData == null || imageData.Length == 0)
{
//Show error msg or return here;
return;
}
var image = new BitmapImage();
using (var ms = new System.IO.MemoryStream(imageData))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
image.Freeze();
}
Add Property in 'Part' class
public byte[] ImageCol { get; set; }
Then from "OpenFileDialog" create Image from selected file. For Example
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG)|*.BMP;*.JPG;*.JPEG;*.GIF;*.PNG",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
if (openFileDialog.ShowDialog()==DialogResult.OK)
{
var imageFromFile = System.Drawing.Image.FromFile(openFileDialog.FileName);
part.ImageCol = imageFromFile.ConvertBitmapImagetoBytes();
}
Convert BitMap to byte[]
public static byte[] ConvertBitmapImagetoBytes(this Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}

How to convert icon (Bitmap) to ImageSource?

I have a button and an image named as image1 in my wpf app. I want add image source of the image1 from a file icon of a location or file path. Here is my code:
using System.Windows;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Icon ico = System.Drawing.Icon.ExtractAssociatedIcon(#"C:\WINDOWS\system32\notepad.exe");
image1.Source = ico.ToBitmap();
}
}
}
And the error is saying
Cannot implicitly convert type 'System.Drawing.Bitmap' to
'System.Windows.Media.ImageSource'
How to solve this problem?
The solution suggested by Farhan Anam will work, but it's not ideal: the icon is loaded from a file, converted to a bitmap, saved to a stream and reloaded from the stream. That's quite inefficient.
Another approach is to use the System.Windows.Interop.Imaging class and its CreateBitmapSourceFromHIcon method:
private ImageSource IconToImageSource(System.Drawing.Icon icon)
{
return Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
new Int32Rect(0, 0, icon.Width, icon.Height),
BitmapSizeOptions.FromEmptyOptions());
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
using (var ico = System.Drawing.Icon.ExtractAssociatedIcon(#"C:\WINDOWS\system32\notepad.exe"))
{
image1.Source = IconToImageSource(ico);
}
}
Note the using block to dispose the original icon after you converted it. Not doing this will cause handle leaks.
The error you get is because you try to assign a bitmap as the source of an image. To rectify that, use this function:
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
like this:
image1.Source = BitmapToImageSource(ico.ToBitmap());

How can I convert a jpg file into a bitmap, using C#?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace convert
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load_1(object sender, EventArgs e)
{
// Image image = Image.FromFile(#"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");
// Set the PictureBox image property to this image.
// ... Then, adjust its height and width properties.
// pictureBox1.Image = image;
//pictureBox1.Height = image.Height;
//pictureBox1.Width = image.Width;
string strFileName = #"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg";
Bitmap bitmap = new Bitmap(strFileName);
//bitmap.Save("testing.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
pictureBox1.Image = bitmap;
pictureBox1.Height = bitmap.Height;
pictureBox1.Width = bitmap.Width;
}
}
}
I am using the above code for converting jpg file into bitmap. It works but I need to know how to stream the jpg image and convert it into bitmap then display the bitmap image with out storing it. I am using c# and vb.net
Try this to convert to Bitmap :
public Bitmap ConvertToBitmap(string fileName)
{
Bitmap bitmap;
using(Stream bmpStream = System.IO.File.Open(fileName, System.IO.FileMode.Open ))
{
Image image = Image.FromStream(bmpStream);
bitmap = new Bitmap(image);
}
return bitmap;
}
Possibly easier:
var bitmap = new Bitmap(Image.FromFile(path));

Categories

Resources