我有一個繪圖應用程序在winforms C#開發,它在整個代碼中使用許多System.Drawing.Bitmap對象。C#winforms代碼到C#wpf代碼
現在我正在用c#將它寫入WPF。我已經完成了近90%的轉換。
來到這個問題......我有被使用的像素
Bitmap result = new Bitmap(img); // img is of System.Drawing.Image
result.SetResolution(img.HorizontalResolution, img.VerticalResolution);
BitmapData bmpData = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.ReadWrite, img.PixelFormat);
int pixelBytes = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat)/8;
System.IntPtr ptr = bmpData.Scan0;
int size = bmpData.Stride * result.Height;
byte[] pixels = new byte[size];
int index = 0;
double R = 0;
double G = 0;
double B = 0;
System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, size);
for (int row = 0; row <= result.Height - 1; row++)
{
for (int col = 0; col <= result.Width - 1; col++)
{
index = (row * bmpData.Stride) + (col * pixelBytes);
R = pixels[index + 2];
G = pixels[index + 1];
B = pixels[index + 0];
.
.// logic code
.
}
}
result.UnlockBits(bmpData);
它使用System.Drawing中的爲宗旨,以遍歷圖像像素下面的代碼。
是否可以在wpf中實現這個功能,並保持簡單?
謝謝。我在計算索引時遇到了麻煩。 [index =(row * bmpData.Stride)+(col * pixelBytes);] ?? – 2010-10-02 08:58:19
@Vinod,stride = img.PixelWidth * bytesPerPixel。 – 2010-10-02 09:28:43