Click here to Skip to main content
14,640,678 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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();
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

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);
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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=....
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100