Copy codeThe Code is as follows: public void CutToF (Stream stream)
{
Image initImage = Image. FromStream (stream, true );
If (initImage. Width <= 480 & initImage. Height <= 480)
{
InitImage. Save ("D: // test.jpg", ImageFormat. Jpeg );
}
Else
{
Int initWidth = initImage. Width;
Int initHeight = initImage. Height;
If (initWidth! = InitHeight)
{
Image pickedImage = null;
Graphics pickedG = null;
If (initWidth> initHeight)
{
PickedImage = new Bitmap (initHeight, initHeight );
PickedG = Graphics. FromImage (pickedImage );
PickedG. InterpolationMode = InterpolationMode. HighQualityBicubic;
PickedG. SmoothingMode = SmoothingMode. HighQuality;
Rectangle fromR = new Rectangle (0, 0, initHeight, initHeight); // (initWidth-initHeight)/2
Rectangle toR = new Rectangle (0, 0, initHeight, initHeight );
PickedG. DrawImage (initImage, fromR );
PickedImage. Save ("D: // jpg // test2.jpg ");
InitWidth = initHeight;
}
InitImage = (Image) pickedImage. Clone ();
PickedG. Dispose ();
PickedImage. Dispose ();
InitImage. Save ("D: // jpg // test1.jpg ");
}
Image resultImage = new Bitmap (150,150 );
Graphics resultG = Graphics. FromImage (resultImage );
ResultG. InterpolationMode = InterpolationMode. HighQualityBicubic;
ResultG. SmoothingMode = SmoothingMode. HighQuality;
ResultG. Clear (Color. White );
ResultG. DrawImage (initImage, new Rectangle (0, 0,100,100), new Rectangle (0, 0, initWidth, initHeight), GraphicsUnit. Pixel );
ImageCodecInfo [] icis = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo ici = null;
Foreach (ImageCodecInfo item in icis)
{
If (item. mimeType = "image/jpeg" | item. mimeType = "image/bmp" | item. mimeType = "image/png" | item. mimeType = "image/gif ")
{
Ici = item;
}
}
EncoderParameters ep = new System. Drawing. Imaging. EncoderParameters (1 );
Ep. Param [0] = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, (long) 100 );
ResultImage. Save ("D: // jpg // test.jpg", ici, ep );
Ep. Dispose ();
ResultG. Dispose ();
ResultImage. Dispose ();
InitImage. Dispose ();
}
}