Home > Net >  C # write a image copy the source code
C # write a image copy the source code

Time:09-22

Want to write code to realize image replication process, the result is not ideal, the effect is as follows:


The code is as follows:
 private void button1_Click (object sender, EventArgs e) 
{
//reference source image
Bitmap source=this. MainPictureBox. Image as Bitmap;//MainPictureBox for the first picture box
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - a, get pixel sets Pixels -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Get the width and height of bitmap
Width=source. The Width;
Height=source. Height;
//Create a rectangle to lock
System, Drawing a Rectangle the rect=new System. Drawing. The Rectangle (0, 0, Width, Height);
//get the total locked pixels count
Int PixelCount=Width * Height;

//Lock bitmap and return bitmap data
BitmapData BitmapData=https://bbs.csdn.net/topics/source.LockBits (the rect, ImageLockMode ReadWrite,
Source. PixelFormat);

//get the source bitmap pixel format size
Int the Depth=System. Drawing. Bitmap. GetPixelFormatSize (source. PixelFormat);
//create byte array to copy the pixel values
Int step=the Depth/8;
Byte [] SorPixels=new byte [PixelCount * step];
IntPtr Iptr=bitmapData. Scan0;

//Copy the data from pointer to an array
Marshal. Copy (Iptr, SorPixels, 0, SorPixels. Length);

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- two, each pixel cycle assignment -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Byte [] desPixels=new byte [PixelCount * step];
For (int Yc=0; Yc & lt; Source. The Height; Vertical Yc++)//
For (int Xr=0; Xr & lt; Source. The Width; Transverse 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. The ToString () + weight_3. ToString () + weigth_4. ToString ());


Color color_left_up=GetPixel (x_left y_up, SorPixels, the Depth);
Color color_left_down=GetPixel (x_left y_down, SorPixels, the Depth);
Color color_right_up=GetPixel (x_right y_up, SorPixels, the Depth);
Color color_right_down=GetPixel (x_right y_down, SorPixels, the 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. Weight_2) R * * weight_3 + (color_right_down. R * weight_1 + color_right_up. Weight_2) R * * 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);

//convert the image coordinate system
//after SetPixel (after. The Width - Xr - 1, Yc, Color. FromArgb (A, R, G, B));
//after SetPixel (Xr, after. The Height - Yc - 1, Color. FromArgb (A, R, G, B));

//Get color components count
Int cCount=the Depth/8;
//Get the start index of the specified pixel
Int I=((Yc * Width) + Xr) * cCount;//* * * * * * I doubt wrong here * * * * * * * * * * * * *
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
}


//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 3, to generate new images -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Bitmap desImage=new Bitmap (source. The Width, the source. Height);
//Get the width and height of bitmap
Width=desImage. Width;
Height=desImage. Height;
//Create a rectangle to lock
System, Drawing a Rectangle Desrect=new System. Drawing. The Rectangle (0, 0, Width, Height);
//get the total locked pixels count
Int DesPixelCount=Width * Height;

//Lock bitmap and return bitmap data
BitmapData DesbitmapData=https://bbs.csdn.net/topics/desImage.LockBits (Desrect, ImageLockMode ReadWrite,
DesImage. PixelFormat);

//get the source bitmap pixel format size
The Depth=System. Drawing. Bitmap. GetPixelFormatSize (desImage. PixelFormat);
//create byte array to copy the pixel values
Step=the 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;
Enclosing the Refresh ();
}
Public Color GetPixel (int x, int y, byte [] Pixels, int the Depth)
{
Color the CLR=Color. The Empty;

//Get color components count
Int cCount=the Depth/8;

//Get the start index of the specified pixel
//int I=(* Width (y) + x) * cCount;
Int I=(* Width (y) + x) * cCount;

If (I & lt; Pixels. Length - cCount)
{
//throw new IndexOutOfRangeException ();
Byte b=Pixels [I];
Byte g=Pixels [I + 1);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related