2014.2.27 merge Images

Source: Internet
Author: User

Public byte [] transpic (string picpath) // convert the image size into bytes

{

Image imgphoto = image. fromfile (picpath );

Int oldx = imgphoto. width;

Int Oldy = imgphoto. height;

 

// New image size

Int Cx = 255;

Int Cy = 255;

 

If (oldx> = Oldy)

Cy = convert. toint32 (255 * convert. todouble (Oldy)/convert. todouble (oldx ));

If (oldx <Oldy)

Cx = convert. toint32 (255 * convert. todouble (oldx)/convert. todouble (Oldy ));

 

Int dx = 255-cx;

Int DY = 255-cy;

 

Image newimage = new Bitmap (255,255 );

Graphics G = graphics. fromimage (newimage );

 

// Clear the canvas and set the background to white

G. Clear (system. Drawing. color. White );

 

// Set the painting quality of the canvas

 

Image Bm = new Bitmap (255,255); // set a blank image first

If (dx> 0) // vertical bar Image

{

// Insert dx/2-width, 255-height blank image on both sides of the original image

// Image Merging

Bm = new Bitmap (dx/2,255 );

G. drawimage (BM, 0, 0, dx/2,255); // Add blank space to the left of the canvas

G. drawimage (imgphoto, dx/2, 0, CX, CY); // Add the original vertical image from dx/2. At this time, there is still a blank dx/2 on the right side of X.

G. drawimage (BM, 255-dx/2, 0, dx/2, 0); // fill in the blank image on the right

}

If (dy> 0)

{

Bm = new Bitmap (255, Dy );

G. drawimage (BM, 0, 0, dx, Dy );

G. drawimage (imgphoto, dx, Dy, CX, CY );

}

 

G. Dispose ();

Imgphoto. Dispose ();

 

Memorystream MS = new memorystream ();

Newimage. Save (MS, system. Drawing. imaging. imageformat. JPEG );

 

Byte [] imagedata = Ms. getbuffer ();

 

Newimage. Dispose ();

Ms. Dispose ();

BM. Dispose ();

 

Return imagedata;

 

}

 

Or

 

// New image size

Int Cx = treatx;

Int Cy = Treaty;

Double r16_9 = 16.0/9.0;

 

Image imgphoto = image. fromfile (picpath );

Int oldx = imgphoto. width;

Int Oldy = imgphoto. height;

 

Int dltx = 0;

Int dlty = 0;

 

If (oldx> = Oldy) // normal horizontal photograph

{

Double oldxyrate = convert. todouble (oldx)/convert. todouble (Oldy );

 

If (oldxyrate> r16_9) // returns the source image width.

{

Int justx = convert. toint32 (Oldy * r16_9 );

Dltx = (oldx-justx)/2; // The number of cropped sides of the source Image

}

Else if (r16_9> oldxyrate) // The Source image is too high.

{

Int Justy = convert. toint32 (oldx/r16_9 );

Dlty = (Oldy-Justy)/2; // The number of cropped up and down images

}

}

If (Oldy> oldx) // the portrait is also taken

{

Cx = treatxx;

Cy = Treaty;

Double r9_16 = 9.0/16.0;

 

Double oldxyrate = convert. todouble (oldx)/convert. todouble (Oldy );

 

 

If (oldxyrate> r9_16) // returns the source image width.

{

Int justx = convert. toint32 (Oldy * r9_16 );

Dltx = (oldx-justx)/2; // The number of cropped sides of the source Image

}

Else if (r9_16> oldxyrate) // The Source image is too high.

{

Int Justy = convert. toint32 (oldx/r9_16 );

Dlty = (Oldy-Justy)/2; // The number of cropped up and down images

}

}

 

 

 

Image newimage = new Bitmap (CX, CY );

Graphics G = graphics. fromimage (newimage );

 

G. drawimage (imgphoto, new rectangle (0, 0, CX, CY), new rectangle (dltx, dlty, imgphoto. width-2 * dltx, imgphoto. height-2 * dlty), graphicsunit. pixel );

G. Dispose ();

Newimage. Dispose ();

Imgphoto. Dispose ();

 

 

Return newimage;

2014.2.27 merge Images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.