繁体   English   中英

从图像读取原始字节数据

[英]read raw byte data from image

我需要从图像获取原始数据,并且正在使用位图LockBits

图像为24BppRgb,但是在创建位图后,位图格式为32BppArgb

知道为什么会这样

    System.Drawing.Bitmap bmp = new Bitmap(image);
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
    IntPtr ptr = bmpData.Scan0;
    int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
    byte[] rgbValues = new byte[bytes];
    Marshal.Copy(ptr, rgbValues, 0, bytes);
    bmp.UnlockBits(bmpData);
    bmp.Dispose();

我怀疑您的原始图片实际上是32bppArgb。 如果是这种情况,为什么不转换为RGB?

var bmp24bppRgb = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

using (var g = Graphics.FromImage(bmp24bppRgb))
{
  g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
}

...,然后使用您的代码访问转换后图像的位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM