c# How to convert pictureBox.Image to Byte Array? [duplicate]
Asked Answered
M

2

5

I looking for fast way Picturebox in image Convert to byte array.

I saw this code but i don't need it. because Picture box of images is the data read from the db. So I don't know ImageFormat

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

So Please let me know if anybody know fast way

Thanks! have a good time!

Metamer answered 10/2, 2015 at 8:0 Comment(2)
You have to "know" the ImageFormat. It is the format it will be saved in and it is up to you to decide. It does not depend on the source format.Isthmian
@Marek, he potentially should actually have the source format with imageIn.PixelFormat property.Vanguard
V
2

This might not be exactly what you're looking for, but it might be useful to you if you're looking for performance in doing some pixel operations .. and I thought it would be worth mentioning it here.

Since you've already loaded the image with Image imageIn you can actually directly access the image buffer without doing any copies hence saving time and resources :

public void DoStuffWithImage(System.Drawing.Image imageIn)
{
    // Lock the bitmap's bits.  
    Rectangle rect = new Rectangle(0, 0, imageIn.Width, imageIn.Height);
    System.Drawing.Imaging.BitmapData bmpData =
                    imageIn.LockBits(rect, System.Drawing.Imaging.ImageLockMode.Read,
                    imageIn.PixelFormat);

    // Access your data from here this scan0,
    // and do any pixel operation with this imagePtr.
    IntPtr imagePtr = bmpData.Scan0;

    // When you're done with it, unlock the bits.
    imageIn.UnlockBits(bmpData);
}

For some more information, look at this MSDN page

ps: This bmpData.Scan0 will of course give you only access to the pixel payload. aka, no headers !

Vanguard answered 10/2, 2015 at 8:46 Comment(1)
parameter missing here Dim picture As Byte() = GetBytes(ListView2.Items(index).SubItems(8).Text) Dim converter As New ImageConverter() PictureBox1.Image = DirectCast(converter.ConvertFrom(picture), Image)Remembrancer
A
5

Try to read this: http://www.vcskicks.com/image-to-byte.php Hope it will help you out.

Edit: I guess that you have your code snip from link that Fung linked. If so, there is the answer for you question right there you just need to scroll down...

2nd Edit (code snippet from page - thanks for info Fedor):

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
Archeozoic answered 10/2, 2015 at 8:26 Comment(0)
V
2

This might not be exactly what you're looking for, but it might be useful to you if you're looking for performance in doing some pixel operations .. and I thought it would be worth mentioning it here.

Since you've already loaded the image with Image imageIn you can actually directly access the image buffer without doing any copies hence saving time and resources :

public void DoStuffWithImage(System.Drawing.Image imageIn)
{
    // Lock the bitmap's bits.  
    Rectangle rect = new Rectangle(0, 0, imageIn.Width, imageIn.Height);
    System.Drawing.Imaging.BitmapData bmpData =
                    imageIn.LockBits(rect, System.Drawing.Imaging.ImageLockMode.Read,
                    imageIn.PixelFormat);

    // Access your data from here this scan0,
    // and do any pixel operation with this imagePtr.
    IntPtr imagePtr = bmpData.Scan0;

    // When you're done with it, unlock the bits.
    imageIn.UnlockBits(bmpData);
}

For some more information, look at this MSDN page

ps: This bmpData.Scan0 will of course give you only access to the pixel payload. aka, no headers !

Vanguard answered 10/2, 2015 at 8:46 Comment(1)
parameter missing here Dim picture As Byte() = GetBytes(ListView2.Items(index).SubItems(8).Text) Dim converter As New ImageConverter() PictureBox1.Image = DirectCast(converter.ConvertFrom(picture), Image)Remembrancer

© 2022 - 2024 — McMap. All rights reserved.