Graphics对象的图像文件 [英] Graphics object to image file
问题描述
我想作物和调整图片的大小。这里是我的code:
I would like to crop and resize my image. Here is my code:
Image image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Cropper/tests/castle.jpg");
// Crop and resize the image.
Rectangle destination = new Rectangle(0, 0, 200, 120);
Graphics graphic = Graphics.FromImage(image);
graphic.DrawImage(image, destination, int.Parse(X1.Value), int.Parse(Y1.Value), int.Parse(Width.Value), int.Parse(Height.Value), GraphicsUnit.Pixel);
现在我认为我得到的裁剪/调整后的图像存储在图形的对象。现在的问题是 - 我怎么把它保存到一个文件
Now I assume that my resulting cropped/resized image is stored in the graphics object. The question is - how do I save it to a file?
推荐答案
这是你获得图形
对象 Graphics.FromImage
为图像的图面。所以,你可以简单地将图像保存对象时你做。
The Graphics
object that you get from Graphics.FromImage
is a drawing surface for the image. So you can simply save the image object when you are done.
string fileName = AppDomain.CurrentDomain.BaseDirectory + "Cropper/tests/castle.jpg");
using (Image image = Image.FromFile(fileName)
{
using (Graphics graphic = Graphics.FromImage(image))
{
// Crop and resize the image.
Rectangle destination = new Rectangle(0, 0, 200, 120);
graphic.DrawImage(image, destination, int.Parse(X1.Value), int.Parse(Y1.Value), int.Parse(Width.Value), int.Parse(Height.Value), GraphicsUnit.Pixel);
}
image.Save(fileName);
}
但是要注意的JPG图像上反复做这未必是一件好事;图像被重新连接,每次codeD及自JPG采用了破坏性的COM pression的方法,你每次失去一些图像质量。我不会担心,如果这是一个曾经每映像操作虽然。
Beware though that doing this repeatedly on a jpg image may not be a good thing; the image is re-encoded each time and since jpg uses a destructive compression method you will lose some image quality each time. I wouldn't worry about that if this is a once-per-image operation though.
这篇关于Graphics对象的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!