我正在尝试使用以下代码将字节数组转换为Bitmap
图像:
using (Graphics g = Graphics.FromImage(tilePage))
g.Clear(Color.Black);
for (int i = 0; i + offset < ROM.Length && i < tilePage.Height; i++)
{
int I = i * tilePage.Width + offset; //+Offset;
for (int j = 0; j < tilePage.Width; j++)
{
if (I + j >= ROM.Length)
goto finishRender;
tilePage.SetPixel(j, i, RPallete.getColor(ROM[I + j]));
}
}
finishRender:
picTileView.Image = tilePage;
但渲染速度太慢(256 * 512分辨率为2-3秒)。如何使它更快,例如在TileLayer中?
P.S。:抱歉英语不好......
答案 0 :(得分:-1)
您可以使用指针。我最近用过这段代码。您可以随意改进
试试这个:
byte siyah=0;
int dizisayisi;
IntPtr baslangic;
byte[] rgbdeger;
Bitmap resim;
Rectangle rct;
BitmapData bmData;
resim = new Bitmap(@"C:\YazilimGrubu.jpg");
rct = new Rectangle(0, 0, resim.Width, resim.Height);
bmData = resim.LockBits(rct, ImageLockMode.ReadWrite, resim.PixelFormat);
baslangic = bmData.Scan0;
dizisayisi = bmData.Stride * resim.Height;
rgbdeger = new byte[dizisayisi];
Marshal.Copy(baslangic, rgbdeger, 0, dizisayisi);
for (int i = 2; i < rgbdeger.Length; i += 3)
{
siyah = (Byte)Math.Abs((Byte.Parse(rgbdeger[i - 2].ToString()) + Byte.Parse(rgbdeger[i - 1].ToString()) + Byte.Parse(rgbdeger[i].ToString())) / 3);
rgbdeger[i - 2] = siyah;
rgbdeger[i - 1] = siyah;
rgbdeger[i] = siyah;
}
Marshal.Copy(rgbdeger, 0, baslangic, dizisayisi);
resim.UnlockBits(bmData);
pctResim.Image = resim;