【问题标题】:c# How to convert pictureBox.Image to Byte Array? [duplicate]c#如何将pictureBox.Image转换为字节数组? [复制]
【发布时间】:2015-02-10 08:00:39
【问题描述】:

我正在寻找快速方法将图片中的Picturebox转换为字节数组。

我看到了这段代码,但我不需要它。因为图片的图片框是从数据库中读取的数据。 所以我不知道ImageFormat

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

所以请告诉我是否有人知道快速方法

谢谢!玩得开心!

【问题讨论】:

  • 您必须“了解” ImageFormat。这是它将保存的格式,由您决定。它不依赖于源格式。
  • @Marek,他可能实际上应该拥有带有imageIn.PixelFormat 属性的源格式。

标签: c# picturebox


【解决方案1】:

尝试阅读:http://www.vcskicks.com/image-to-byte.php 希望对你有所帮助。

编辑:我猜你的代码是从 Fung 链接的链接中截取的。如果是这样,您只需向下滚动即可找到您问题的答案...

第二次编辑(页面中的代码 sn-p - 感谢 Fedor 提供的信息):

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

【讨论】:

    【解决方案2】:

    这可能不是您正在寻找的,但如果您正在寻找执行某些像素操作的性能,它可能对您有用.. 我认为这是值得的在这里提到它。

    由于您已经使用Image imageIn 加载了图像,您实际上可以直接访问图像缓冲区而无需进行任何复制,从而节省时间和资源:

    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);
    }
    

    有关更多信息,请查看此MSDN 页面

    ps:这个 bmpData.Scan0 当然只会让您访问像素有效负载。又名,没有标题!

    【讨论】:

    • 这里缺少参数Dim picture As Byte() = GetBytes(ListView2.Items(index).SubItems(8).Text) Dim converter As New ImageConverter() PictureBox1.Image = DirectCast(converter.ConvertFrom(picture), Image)
    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2014-06-14
    • 2013-07-23
    • 1970-01-01
    • 2013-03-11
    • 2013-03-03
    • 2011-09-16
    相关资源
    最近更新 更多