使位图渲染更快

时间:2014-09-14 13:38:12

标签: c# bitmap

我正在尝试使用以下代码将字节数组转换为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。:抱歉英语不好......

1 个答案:

答案 0 :(得分:-1)

您可以使用指针。我最近用过这段代码。您可以随意改进

  1. 添加名为pctResim
  2. 的图片框
  3. 检查允许项目属性不安全
  4. 试试这个:

    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;