C# 图片剪切与缩小的实例
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();
}
}