简体   繁体   English

System.Drawing.Bitmap到字节数组

[英]System.Drawing.Bitmap to Byte Array

I have a Bitmap from a resource and need to put it in a byte array. 我有一个资源的位图,需要将其放在字节数组中。 I have a SetPixel Methode for manipulating pixels in my Byte Array. 我有一个SetPixel方法来处理字节数组中的像素。 That works all well. 那很好。 What I want to do, is to load first a picture in the array that I can manipulate it later. 我想做的是先将图片加载到数组中,以后再进行操作。

I tried quite a lot to acomplish that. 我做了很多尝试来实现这一目标。 First what I have: 首先我有:

var resources = new ComponentResourceManager(typeof(Game));
var bmp = (Bitmap)(resources.GetObject("imgBall.Image"));

the only so far not giving me an error message (but displaying the picutre wrong) is this: 到目前为止,唯一没有给我错误消息(但显示的图片错误)的是:

width = (int)bmp.Width;
height = (int)bmp.Height;
System.Windows.Media.PixelFormat pf = PixelFormats.Rgb24;
rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] pixelData = new byte[rawStride * height];    

MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
pixelData = ms.GetBuffer();

then I tried solutions I could find in stack overflow already: 然后我尝试了可以​​在堆栈溢出中找到的解决方案:

ImageConverter imgConverter = new ImageConverter();
pixelData = (System.Byte[])imgConverter.ConvertTo(bmp, Type.GetType("System.Byte[]"));

This changes the size of pixelData, so that I cannot use it anymore... 这会更改pixelData的大小,因此我将无法再使用它...

using (MemoryStream memory = new MemoryStream())
            {
                bmp.Save(memory, ImageFormat.Bmp);
                memory.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                memory.Seek(0, SeekOrigin.Begin);
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                int stride = width * ((bitmapImage.Format.BitsPerPixel + 7) / 8);                                

                bitmapImage.CopyPixels(pixelData, stride, 0);                
            }

here at the CopyPixels line I am confused: when I take rawStride from above (84) I get an error message it needs to be at lease 112. When I calculate stride for bitmapImage (112) it complains it needs to be minimum 3136. I even tried to enter the number of stride manually it compains allways it needs to be bigger. 在这里,在CopyPixels行中,我感到困惑:当我从上方(84)取rawStride时,我收到一条错误消息,要求它的租约是112。当我计算bitmapImage (112)的步幅时,它抱怨它的最小值必须为3136。甚至尝试手动输入步幅数,反而它总是需要更大。

I have the feeling that I understand something completely wrong here. 我感觉自己在这里完全理解错误。

public string BitmapToBase64(BitmapImage bi)
{
  MemoryStream ms = new MemoryStream();
  PngBitmapEncoder encoder = new PngBitmapEncoder();
  encoder.Frames.Add(BitmapFrame.Create(bi));
  encoder.Save(ms);
  byte[] bitmapdata = ms.ToArray();

  return Convert.ToBase64String(bitmapdata);
}

This should do the thing. 这应该做的事情。 Just return the byteArray if that is what you want. 如果需要的话,只需返回byteArray即可。

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

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