Retrieve Image from SQL Server & set to PictureBox Image Property - c#

I want to retrieve image from SQL Server(Database) using Entity Framework and set it to PictureBox Image Property
I have maked a user control & inherit it to PictureBox (Name:DisplayImage)
public static void LoadDisplay(Guid? DisplayID, string Name, byte[] image)
{
DisplayImage objDisplayImage = new DisplayImage();
DisplayList.Add(objDisplayImage);
objDisplayImage.Name = Name;
MemoryStream ms = new MemoryStream(image);
Image myImage = Image.FromStream(ms);
objDisplayImage.Image = myImage;
objDisplayImage.DisplayID = DisplayID;
PlayerForm.Instance.Controls.Add(objDisplayImage);
}
but Image is not loading on PictureBox

Try this code
try
{
// get image from object
byte[] _ImageData = new byte[0];
_ImageData = (byte[])_SqlRetVal;
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_ImageData);
_Image = System.Drawing.Image.FromStream(_MemoryStream);
}
catch (Exception _Exception)
{
// Error occurred while trying to create image
// send error message to console (change below line to customize error handling)
Console.WriteLine(_Exception.Message);
return null;
}

Related

Convert address of PictureBox image source

string address = PictureBox.Location;
give the location of Picturebox image source.
How get address of Picturebox's image source in this order:
C:\\ABC\\XYX\\image.png
The PictureBox load with:
using (MySqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
using (MemoryStream stream = new MemoryStream())
{
if (dr["IMAGE"] != DBNull.Value)
{
byte[] image = (byte[])dr["IMAGE"];
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
pictureBox.Image = bitmap;
}
}
}
}
I did try this one and doesn't help.
string strFileName = PictureBox.ImageLocation + #"\ImageOne.JPG";
Also tried this:
pictureBox.Image.Save("D:\\ImageOne.JPG", ImageFormat.Jpeg);
Gives me this error:
A generic error occurred in GDI+. at System.Drawing.Image.Save
Thanks.

System.NullReferenceException: Object reference not set to an instance of an object

In this code i wanted to save the image in picturebox1 in database using linqtosql ..... but getting some exceptions in converting a image to byte array
Exception is "System.NullReferenceException: Object reference not set to an instance of an object."
private void button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext dc = new DataClasses1DataContext();
try
{
string signname = textBox1.Text;
string imageurl = textBox2.Text;
pictureBox1.ImageLocation = imageurl;
// byte[] file_byte = new byte[1000];
// Image newimage = new Image(pictureBox1.Image);
///Error comes here
byte[] file_byte = ImageToByteArray(pictureBox1.Image);
System.Data.Linq.Binary file_binary = new System.Data.Linq.Binary(file_byte);
Sign_Table obj = new Sign_Table()
{
Sign_Name = signname,
Sign_Image = file_binary,
};
dc.Sign_Tables.InsertOnSubmit(obj);
}
finally
{
dc.SubmitChanges();
}
}
private byte[] ImageToByteArray(Image imageIn )
{
using (MemoryStream ms = new MemoryStream())
{
// Error comes here
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
I suspect the problem is that pictureBox.Image is null when you reference it. You are setting pictureBox.ImageLocation but not actually loading the image.
Add a call to pictureBox.Load() immediately after setting pictureBox.ImageLocation.
When you create the Memorystream, you have to associate with bytes[], for example
byte[] buf = new byte[4096];
Then you could you
using (MemoryStream ms = new MemoryStream(buf))
So calculate an upper bound for the image size in bytes, and then associate a MemoryStream to an array containing that many bytes.

Loading image into panel from database

My application saves image data in database and when loading information into form, it loads image into panel. This is working fine. But in case if user doesn't want to save image, I've inserted '0' in image field (varbinary) in database. While loading this information (0x00000000) from database it throws the following exception:
"Parameter is not valid."
Here I am giving some pieces of code:
Saving image in database:
if(user has selected an image)
{
Byte[] imageBytes = File.ReadAllBytes(imagePatient);
sqlCommand.Parameters.AddWithValue("Img", imageBytes); //Img is database column for images
}
else
{
sqlCommand.Parameters.AddWithValue("Img", 0);
}
Loading image from database:
Byte[] imageData = new Byte[0];
imageData = (Byte[])(dataSet.Tables["Patients"].Rows[0]["Img"]);
MemoryStream stream = new MemoryStream(imageData);
panelImage.BackgroundImage = Image.FromStream(stream);
I've tried to put some check which will allow to load BackgroundImage if and only if data is not of the zeros form.
Please tell me how can I solve this issue?
I would store a NULL instead of 0.
if(user has selected an image)
{
Byte[] imageBytes = File.ReadAllBytes(imagePatient);
sqlCommand.Parameters.AddWithValue("Img", imageBytes); //Img is database column for images
}
else
{
sqlCommand.Parameters.Add("Img", SqlDbType.VarBinary, -1 );
sqlCommand.Parameters["Img"].Value = DbNull.Value;
}
And then use an if statement to check if it's non-null:
Byte[] imageData = new Byte[0];
imageData = (Byte[])(dataSet.Tables["Patients"].Rows[0]["Img"]);
if(imageData != null)
{
MemoryStream stream = new MemoryStream(imageData);
panelImage.BackgroundImage = Image.FromStream(stream);
}
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

A generic error occurred in GDI+

I loaded an image into a Picture Box using:
picturebox1.Image = Image.FromFile()
and I save it by using:
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);
It works perfectly fine when creating a new file, but when I try to replace the existing image, I get thrown the following runtime error:
A generic error occurred in GDI+
So what can I do to solve this problem??
That because the image file is used by your picturebox1.Image, try to save it to different file path instead:
picturebox1.Image = Image.FromFile(FileName);
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(#"New File Name", ImageFormat.Bmp);
Edit: You could also add a copy from the image at the first place like:
picturebox1.Image = new Bitmap(Image.FromFile(FileName));
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);//no error will occurs here.
The FromFile method locks the file, so use the Image.FromStream() method for reading the image:
byte[] bytes = System.IO.File.ReadAllBytes(filename);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(ms);
Then save like you were before.
This can also happen if the path does not exist.
You could create the dir with:
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(FileName));
When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated. http://support.microsoft.com/?id=814675
A generic error occurred in GDI+, JPEG Image to MemoryStream:
Image.Save(..) // throws a GDI+ exception because the memory stream is closed
http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html
EDIT: Just writing from memory. Saving to an 'intermediary' MemoryStream should work:
For example, replace this:
Bitmap newBitmap = new Bitmap(thumbBMP);
thumbBMP.Dispose();
thumbBMP = null;
newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
with something like:
string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
thumbBMP.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
try this.
picturebox1.Image = Image.FromFile(FileName);
Bitmap bm = new Bitmat(pictureBox1.Image);
Image img = (Image)b;
img.Save(FileName, ImageFormat.Bmp);
Just like #Jalal Aldeen Saa'd said, the picture box is using the file and locked from file replacement.
//unlock file by clearing it from picture box
if (picturebox1.Image != null)
{
picturebox1.Image.Dispose();
picturebox1.Image = null;
}
//put back the picture inside the pictureBox?
try this it will work
public void SavePicture()
{
Bitmap bm = new Bitmap(this.myBitmap)
bm.Save("Output\\out.bmp" ,System.Drawing.Imaging.ImageFormat.Bmp );
}
This can also happen if you forget to add the filename:
bm.Save(#"C:\Temp\Download", System.Drawing.Imaging.ImageFormat.Png);
And can be fixed by adding the file name:
bm.Save(#"C:\Temp\Download\Image.png", System.Drawing.Imaging.ImageFormat.Png);
Note: You don't actually have to add the extension for it to work.
Try this:
private void LoadPictureBoxWithImage( string ImagePath)
{
Stream objInputImageStream = null;
BitmapData bmdImageData = null;
Bitmap bmpSrcImage = null, bmTemp = null;
byte[] arrImageBytes = null;
int bppModifier = 3;
try
{
objInputImageStream = new MemoryStream();
using (FileStream objFile = new FileStream(ImagePath, FileMode.Open, FileAccess.Read))
{
objFile.CopyTo(objInputImageStream);
}
bmpSrcImage = new Bitmap(objInputImageStream);
bppModifier = bmpSrcImage.PixelFormat == PixelFormat.Format24bppRgb ? 3 : 4;
//reda from byte[] to bitmap
bmdImageData = bmpSrcImage.LockBits(new Rectangle(0, 0, bmpSrcImage.Width, bmpSrcImage.Height), ImageLockMode.ReadOnly, bmpSrcImage.PixelFormat);
arrImageBytes = new byte[Math.Abs(bmdImageData.Stride) * bmpSrcImage.Height];
System.Runtime.InteropServices.Marshal.Copy(bmdImageData.Scan0, arrImageBytes, 0, arrImageBytes.Length);
bmpSrcImage.UnlockBits(bmdImageData);
pbSetup.Image = (Bitmap)bmpSrcImage.Clone();
pbSetup.Refresh();
}
catch (Exception ex)
{
throw new Exception("Error in Function " + System.Reflection.MethodInfo.GetCurrentMethod().Name + "; " + ex.Message);
}
finally
{
if (objInputImageStream != null)
{
objInputImageStream.Dispose();
objInputImageStream = null;
}
if (bmdImageData != null)
{
bmdImageData = null;
}
if (bmpSrcImage != null)
{
bmpSrcImage.Dispose();
bmpSrcImage = null;
}
if (bmTemp != null)
{
bmTemp.Dispose();
bmTemp = null;
}
if (arrImageBytes != null)
{
arrImageBytes = null;
}
}
}
A generic error occurred in GDI+
I also faced the same issue. I tried so many ways to fix this issue. Finally, I found a place where I have gone wrong. The problem is that I used space in the file path, which is not acceptable. Now it is working fine after removing the space in front of C after the apostrophe:
"SupplyItems":"C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"
instead... I used below one.
"SupplyItems":" C:\\inetpub\\HIBMS_Ver1\\BarcodeImages\\Supply\\"
Minor mistake but took a long time to find and to fix it.
Note that images created by Image.Clone() will still cause GDI+ errors as shown by the BAD code below, you must use the Image.FromStream() method for reading the image as shown in the solution on this page.
//BAD CODE: the image we will try to save AFTER the original image has been cloned and disposed
Image clonedImage;
//load image from file, clone it then dispose
using (var loadedFromDiskImage = Image.FromFile(filePath))
{
clonedImage = (Image) loadedFromDiskImage.Clone();
}
//you might think the new image can be saved given the original is disposed
//but this doesn't seem to be the way Clone() works
//expect GDI+ error in line below:
clonedImage.Save(filePath);

How to convert varBinary into image or video when retrieved from database in C#

I am using visual studio 2010, (desktop application) and using LINQ to SQL to save image/video or audio files to database in dataType VarBinary (MAX). This I can do... Problem is, I can't get them and display them in xaml because I can't get the converting part correct. Here is what I have so far (though its not working);
private void bt_Click (object sender, RoutedEventArgs e)
{
databaseDataContext context = new databaseDataContext();
var imageValue = from s in context.Images
where s.imageID == 2
select s.imageFile;
value = imageValue.Single().ToString();
//convert to string and taking down to next method to get converted in image
}
public string value { get; set; }
public object ImageSource //taking from http://stackoverflow.com/
{
get
{
BitmapImage image = new BitmapImage();
try
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(value, UriKind.Absolute);
image.EndInit();
Grid.Children.Add(image);
}
catch { return DependencyProperty.UnsetValue; } return image;
}
}
I not even sure if I am on the correct track? And I am assuming that video or audio is quite similar methods?
Since your image is stored in binary format in the database, you want to "stream" this into an image object by leveraging the MemoryStream object.
Looking at your code, your solution will look something like this:
BitmapImage bmpImage = new BitmapImage();
MemoryStream msImageStream = new MemoryStream();
msImageStream.Write(value, 0, value.Length);
bmpCardImage.BeginInit();
bmpCardImage.StreamSource = new MemoryStream(msImageStream.ToArray());
bmpCardImage.EndInit();
image.Source = bmpCardImage;
It's very easy, if you have a binary data and want to create an Image object, use this code:
public Image BinaryToImage(byte[] binaryData)
{
MemoryStream ms = new MemoryStream(binaryData);
Image img = Image.FromStream(ms);
return img;
}
If you already have the bytes, to verify that what you saved is correct you can save the bytes to a file and open it....
string tempFile = Path.GetTempFileName();
MemoryStream ms = new MemoryStream(bytes); //bytes that was read from the db
//Here I assume that you're reading a png image, you can put any extension you like is a file name
FileStream stream = new FileStream(tempFile + ".png", FileMode.Create);
ms.WriteTo(stream);
ms.Close();
stream.Close();
//And here we open the file with the default program
Process.Start(tempFile + ".png");
And later you can use the answer of Dillie-O and stream....
Dillie-O's code makes for a very nice extension method:
// from http://stackoverflow.com/questions/5623264/how-to-convert-varbinary-into-image-or-video-when-retrieved-from-database-in-c:
public static BitmapImage ToImage(this Binary b)
{
if (b == null)
return null;
var binary = b.ToArray();
var image = new BitmapImage();
var ms = new MemoryStream();
ms.Write(binary, 0, binary.Length);
image.BeginInit();
image.StreamSource = new MemoryStream(ms.ToArray());
image.EndInit();
return image;
}

Categories