[英]Converting a Bitmap into RGB Pixels
我正在使用以下代碼,並且試圖將位圖轉換為RGB像素(0-255)的數組。 在下面的代碼中,我對需要幫助的偽代碼發表了評論:
private class Rgb
{
public byte Red { get; set; }
public byte Green { get; set; }
public byte Blue { get; set; }
public Rgb(byte red, byte green, byte blue)
{
Red = red;
Green = green;
Blue = blue;
}
}
private class MyImage
{
private Rgb[] _pixels;
private readonly int _width;
private readonly int _height;
public int Width
{
get { return _width; }
}
public int Height
{
get { return _height; }
}
public Rgb[] Pixels
{
get { return _pixels; }
}
public MyImage(int width, int height)
{
_width = width;
_height = height;
_pixels = new Rgb[_width*_height];
}
public static MyImage FromBitmap(Bitmap bmp)
{
var myImage = new MyImage(bmp.Width, bmp.Height);
int i = 0;
// foreach(pixel in myImage)
// {
// myImage._pixels[i] = new Rgb(pixel.red, pixel.green, pixel.blue);
// i++;
// }
return myImage;
}
public static Bitmap ToBitmap(MyImage myImage)
{
var bitmap = new Bitmap(myImage.Width, myImage.Height);
int i = 0;
// foreach(pixel in myImage)
// {
// bitmap.pixel[i].red = myImage.Pixels[i].Red;
// bitmap.pixel[i].green = myImage.Pixels[i].Green;
// bitmap.pixel[i].blue = myImage.Pixels[i].Blue;
// i++;
// }
return bitmap;
}
我被困在如何從位圖獲取像素數據。 此外,我還需要能夠從像素數據創建位圖。 任何幫助,將不勝感激!
查看此鏈接: http : //msdn.microsoft.com/zh-cn/library/system.drawing.imaging.bitmapdata.aspx小心使用24位圖像,因為(如果我記得很好的話)它們需要與行對齊4字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.