Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need help in converting image(picturebox.resourse) to byte array (byte[]) in WPF
id used this code : but it cannot resolve second line
"Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);"
my code is :
MemoryStream ms = new MemoryStream();
picturebox.Resources.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic_arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(pic_arr, 0, pic_arr.Length);


Please If You can help me I would be grateful.
Posted

C#
Bitmap bitmapImage = picturebox.Resourses;
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}
 
Share this answer
 
v2
private void GetImage(byte[] pic)
{
byte[] data = (byte[])pic;
MemoryStream strm = new MemoryStream();
strm.Write(data, 0, data.Length);
 
strm.Position = 0;
 
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
 
BitmapImage bi = new BitmapImage();
 
bi.BeginInit();
 
MemoryStream ms = new MemoryStream();
 
img.Save(ms, ImageFormat.Bmp);
 
ms.Seek(0, SeekOrigin.Begin);
 
bi.StreamSource = ms;
 
bi.EndInit();
 
pbStuPic.Source = bi;
}
 

 

 

we caled like this
 var bits = getJPGFromImageControl(pbStuPic.Source as BitmapImage);
 
Share this answer
 
it can not resolve (bitmapImage) this solution in wpf .net 4.5
i am using vs2012


the problem is :
i have picture box in main form and i need to put it in byte[] to send it to database
i access to the picture in picturebox like this
picturebox.Resourses=....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900