this.BackColor = Color.FromArgb(0, 0, 0); Bitmap bmp = new Bitmap(ScreenWidth, ScreenHeight); // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. // This code is specific to a bitmap with 24 bits per pixels. int bytes = bmp.Width * bmp.Height * 3; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // 间隔一个子像素为255 //第一个:(255,0,255) 第二个:(0,255,0) 如此交替重复. for (int counter = 0; counter < rgbValues.Length; counter += 2) rgbValues[counter] = 255; // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. bmp.UnlockBits(bmpData); // Draw the modified image. e.Graphics.DrawImage(bmp, 0,0); //为什么画出来还是全黑一片啊?......
循环又慢
可用Bitmap.LockBits获取BitmapData数据后处理
矩阵变换
全部的点都先画成(0,0,0),利用变换矩阵就可以了(GDI+编程)
思路2:
在protected override void OnPaint(PaintEventArgs e)事件里采取双缓存
{
public Form1()
{
InitializeComponent(); Bitmap bmp = new Bitmap(2, 2);
bmp.SetPixel(0, 0, Color.Black);
bmp.SetPixel(1, 1, Color.Black);
bmp.SetPixel(1, 0, Color.White);
bmp.SetPixel(0, 1, Color.White); this.BackgroundImage = bmp;
}
}
this.BackColor = Color.FromArgb(0, 0, 0);
Bitmap bmp = new Bitmap(ScreenWidth, ScreenHeight); // Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat); // Get the address of the first line.
IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmp.Width * bmp.Height * 3;
byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // 间隔一个子像素为255
//第一个:(255,0,255) 第二个:(0,255,0) 如此交替重复.
for (int counter = 0; counter < rgbValues.Length; counter += 2)
rgbValues[counter] = 255; // Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits.
bmp.UnlockBits(bmpData); // Draw the modified image.
e.Graphics.DrawImage(bmp, 0,0);
//为什么画出来还是全黑一片啊?......