정수 배열과 비트 맵이 빠른 PictureBox 편집을 위해 (느린 SetPixel 명령을 사용하지 않고) C# winforms 프로젝트의 일부로 조화롭게 작동하려고합니다.C# 컴퓨터 게임 (WinForms)을위한 간단하고 빠른 실시간 그래픽
나는 Button과 PictureBox, aformentioned 버튼의 클릭 이벤트와 Form의 closing 이벤트를 추가했습니다. 양식에 대한
코드과 같이 지금 : 실행하면 예상대로 검은 색 이미지가 처음에 존재
public partial class Form1 : Form
{
uint[] _Pixels { get; set; }
Bitmap _Bitmap { get; set; }
GCHandle _Handle { get; set; }
IntPtr _Addr { get; set; }
public Form1()
{
InitializeComponent();
int imageWidth = 100; //1920;
int imageHeight = 100; // 1080;
PixelFormat fmt = PixelFormat.Format32bppRgb;
int pixelFormatSize = Image.GetPixelFormatSize(fmt);
int stride = imageWidth * pixelFormatSize;
int padding = 32 - (stride % 32);
if (padding < 32)
{
stride += padding;
}
_Pixels = new uint[(stride/32) * imageHeight + 1];
_Handle = GCHandle.Alloc(_Pixels, GCHandleType.Pinned);
_Addr = Marshal.UnsafeAddrOfPinnedArrayElement(_Pixels, 0);
_Bitmap = new Bitmap(imageWidth, imageHeight, stride/8, fmt, _Addr);
pictureBox1.Image = _Bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < _Pixels.Length; i++)
{
_Pixels[i] = ((uint)(255 | (255 << 8) | (255 << 16) | 0xff000000));
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_Addr = IntPtr.Zero;
if (_Handle.IsAllocated)
{
_Handle.Free();
}
_Bitmap.Dispose();
_Bitmap = null;
_Pixels = null;
}
}
.
버튼을 클릭 할 때 이미지가 흰색으로 바뀌지 만 항목이 누락되었습니다.
나는 무엇을 잊는가?