4,819
社区成员




private void button1_Click(object sender, EventArgs e)
{
// 引用源图像
Bitmap source = this.MainPictureBox.Image as Bitmap;//MainPictureBox为第一个图片框
//---------------------一、获取像素集Pixels-----------------------------------
// Get width and height of bitmap
Width = source.Width;
Height = source.Height;
// Create rectangle to lock
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, Width, Height);
// get total locked pixels count
int PixelCount = Width * Height;
// Lock bitmap and return bitmap data
BitmapData bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);
// get source bitmap pixel format size
int Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
// create byte array to copy pixel values
int step = Depth / 8;
byte[] SorPixels = new byte[PixelCount * step];
IntPtr Iptr = bitmapData.Scan0;
// Copy data from pointer to array
Marshal.Copy(Iptr, SorPixels, 0, SorPixels.Length);
//--------------------------二、每个像素循环赋值--------------------------
byte[] desPixels = new byte[PixelCount * step];
for (int Yc = 0; Yc < source.Height; Yc++) //纵
for (int Xr = 0; Xr < source.Width; Xr++) //横
{
int x_left = Xr;
int x_right = x_left + 1;
int y_down = Yc;
int y_up = y_down + 1;
double weight_1 = y_up - Yc;
double weight_2 = Yc - y_down;
double weight_3 = x_right - Xr;
double weigth_4 = Xr - x_left;
//MessageBox.Show(weight_1.ToString() + weight_2.ToString() + weight_3.ToString() + weigth_4.ToString());
Color color_left_up = GetPixel(x_left, y_up, SorPixels, Depth);
Color color_left_down = GetPixel(x_left, y_down, SorPixels, Depth);
Color color_right_up = GetPixel(x_right, y_up, SorPixels, Depth);
Color color_right_down = GetPixel(x_right, y_down, SorPixels, Depth);
byte A = (byte)((color_left_down.A * weight_1 + color_left_up.A * weight_2) * weight_3 + (color_right_down.A * weight_1 + color_right_up.A * weight_2) * weigth_4);
byte R = (byte)((color_left_down.R * weight_1 + color_left_up.R * weight_2) * weight_3 + (color_right_down.R * weight_1 + color_right_up.R * weight_2) * weigth_4);
byte G = (byte)((color_left_down.G * weight_1 + color_left_up.G * weight_2) * weight_3 + (color_right_down.G * weight_1 + color_right_up.G * weight_2) * weigth_4);
byte B = (byte)((color_left_down.B * weight_1 + color_left_up.B * weight_2) * weight_3 + (color_right_down.B * weight_1 + color_right_up.B * weight_2) * weigth_4);
//转换为图像坐标系
// after.SetPixel(after.Width - Xr - 1, Yc, Color.FromArgb(A, R, G, B));
//after.SetPixel(Xr, after.Height - Yc-1, Color.FromArgb(A, R, G, B));
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((Yc * Width) + Xr) * cCount; //******我怀疑这里出错了*************
desPixels[i] = B;
desPixels[i + 1] = G;
desPixels[i + 2] = R;
//after.SetPixel(Xr, Yc, Color.FromArgb(A, R, G, B));
////after.SetPixel(Yc, Xr,Color.FromArgb(A, R, G, B));
//desPixels
}
//--------------------------三、生成新图像--------------------------
Bitmap desImage = new Bitmap(source.Width, source.Height);
// Get width and height of bitmap
Width = desImage.Width;
Height = desImage.Height;
// Create rectangle to lock
System.Drawing.Rectangle Desrect = new System.Drawing.Rectangle(0, 0, Width, Height);
// get total locked pixels count
int DesPixelCount = Width * Height;
// Lock bitmap and return bitmap data
BitmapData DesbitmapData = desImage.LockBits(Desrect, ImageLockMode.ReadWrite,
desImage.PixelFormat);
// get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(desImage.PixelFormat);
// create byte array to copy pixel values
step = Depth / 8;
Iptr = DesbitmapData.Scan0;
Marshal.Copy(desPixels, 0, Iptr, desPixels.Length);
// Unlock bitmap data
desImage.UnlockBits(DesbitmapData);
source.UnlockBits(bitmapData);
this.TestPic.Image = desImage;
this.Refresh();
}
public Color GetPixel(int x, int y, byte[] Pixels, int Depth)
{
Color clr = Color.Empty;
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
//int i = ((y * Width) + x) * cCount;
int i = ((y * Width) + x) * cCount;
if (i < Pixels.Length - cCount)
{
//throw new IndexOutOfRangeException();
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}
else
{ Console.WriteLine(i.ToString()); }
return clr;
}
// 当Stride为正数:
Scan0 -> ************************
************************
************************
************************
// 当Stride为负数,Scan0指向最后一行:
************************
************************
************************
Scan0 -> ************************
private void button1_Click(object sender, EventArgs e)
{
// 引用源图像
Bitmap source = this.MainPictureBox.Image as Bitmap;//MainPictureBox为第一个图片框
//---------------------一、获取像素集Pixels-----------------------------------
// Get width and height of bitmap
int reWidth = source.Width;
int reHeight = source.Height;
// Create rectangle to lock
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, reWidth, reHeight);
// get total locked pixels count
int PixelCount = reWidth * reHeight;
// Lock bitmap and return bitmap data
BitmapData bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat);
// get source bitmap pixel format size
int Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
// create byte array to copy pixel values
int step = Depth / 8;
byte[] SorPixels = new byte[PixelCount * step];
IntPtr Iptr = bitmapData.Scan0;
// Copy data from pointer to array
Marshal.Copy(Iptr, SorPixels, 0, SorPixels.Length);
//--------------------------二、每个像素循环赋值--------------------------
byte[] desPixels = new byte[PixelCount * step];
for (int Yc = 0; Yc < source.Height; Yc++) //纵
{
for (int Xr = 0; Xr < source.Width; Xr++) //横
{
int x_left = Xr;
int x_right = x_left + 1;
int y_down = Yc;
int y_up = y_down + 1;
double weight_1 = y_up - Yc;
double weight_2 = Yc - y_down;
double weight_3 = x_right - Xr;
double weigth_4 = Xr - x_left;
//MessageBox.Show(weight_1.ToString() + weight_2.ToString() + weight_3.ToString() + weigth_4.ToString());
Color color_left_up = GetPixel(x_left, y_up, SorPixels, Depth);
Color color_left_down = GetPixel(x_left, y_down, SorPixels, Depth);
Color color_right_up = GetPixel(x_right, y_up, SorPixels, Depth);
Color color_right_down = GetPixel(x_right, y_down, SorPixels, Depth);
byte A = (byte)((color_left_down.A * weight_1 + color_left_up.A * weight_2) * weight_3 + (color_right_down.A * weight_1 + color_right_up.A * weight_2) * weigth_4);
byte R = (byte)((color_left_down.R * weight_1 + color_left_up.R * weight_2) * weight_3 + (color_right_down.R * weight_1 + color_right_up.R * weight_2) * weigth_4);
byte G = (byte)((color_left_down.G * weight_1 + color_left_up.G * weight_2) * weight_3 + (color_right_down.G * weight_1 + color_right_up.G * weight_2) * weigth_4);
byte B = (byte)((color_left_down.B * weight_1 + color_left_up.B * weight_2) * weight_3 + (color_right_down.B * weight_1 + color_right_up.B * weight_2) * weigth_4);
//转换为图像坐标系
// after.SetPixel(after.Width - Xr - 1, Yc, Color.FromArgb(A, R, G, B));
//after.SetPixel(Xr, after.Height - Yc-1, Color.FromArgb(A, R, G, B));
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((Yc * reWidth) + Xr) * cCount; //******我怀疑这里出错了*************
desPixels[i] = B;
desPixels[i + 1] = G;
desPixels[i + 2] = R;
}
}
//--------------------------三、生成新图像--------------------------
Bitmap desImage = new Bitmap(source.Width, source.Height, source.PixelFormat);
// Get width and height of bitmap
reWidth = desImage.Width;
reHeight = desImage.Height;
// Create rectangle to lock
System.Drawing.Rectangle Desrect = new System.Drawing.Rectangle(0, 0, reWidth, reHeight);
// get total locked pixels count
int DesPixelCount = reWidth * reHeight;
// Lock bitmap and return bitmap data
BitmapData DesbitmapData = desImage.LockBits(Desrect, ImageLockMode.ReadWrite,
desImage.PixelFormat);
// get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(desImage.PixelFormat);
// create byte array to copy pixel values
step = Depth / 8;
Iptr = DesbitmapData.Scan0;
Marshal.Copy(desPixels, 0, Iptr, desPixels.Length);
// Unlock bitmap data
desImage.UnlockBits(DesbitmapData);
source.UnlockBits(bitmapData);
this.TestPic.Image = desImage;
//this.Refresh();
}
public Color GetPixel(int x, int y, byte[] Pixels, int Depth)
{
Color clr = Color.Empty;
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
//int i = ((y * Width) + x) * cCount;
int i = ((y * Width) + x) * cCount;
if (i < Pixels.Length - cCount)
{
//throw new IndexOutOfRangeException();
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
clr = Color.FromArgb(r, g, b);
}
else
{ Console.WriteLine(i.ToString()); }
return clr;
}
未搞定,找不出原因,也没听懂原因,只能处理到这里了。
因为从未用C#作过图像处理,搞了两天没搞定(不夸张,真2天)
此问题留存
留作以后有时间研究。