拖不托管是浮云——飘过托管的边界
写这篇博文为了说明如何"托管"与'"非托管"互用问题。具体来讲包括:如何在托管代码中使用非托管代码、如何在托管代码中使用非托管dll、如何在非托管代码中使用托管dll以及托管代码。直接给出最直接的描述---代码。1.托管代码中使用非托管代码
给出个可行示例,简单的说明下下面这段代码的功能--“灰度化”图像。
//托管代码调用非托管代码//DebugLZQ以前写的//unsafe{}中代码为非托管代码private void pointer_Click(object sender, EventArgs e) { if (curBitmap != null) { myTimer.ClearTimer(); myTimer.Start(); Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); byte temp = 0; unsafe { byte* ptr = (byte*)(bmpData.Scan0); for (int i = 0; i < bmpData.Height; i++) { for (int j = 0; j < bmpData.Width; j++) { temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]); ptr[0] = ptr[1] = ptr[2] = temp; ptr += 3; } ptr += bmpData.Stride - bmpData.Width * 3; } } curBitmap.UnlockBits(bmpData); myTimer.Stop(); timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; Invalidate(); } }
页:
[1]