如何设置图片的 X 和 Y 以保存图像 C#
How to Set my X and Y Of Pic To save an image C#
我有一张照片(这张照片有 X:467 Y:300)我只需要从 X:0 Y:0 到 X:200 Y:45 我使用此代码进行裁剪,但我丢失了 X:0 Y:0 到 X:200 Y:45 的数据,我不知道该怎么做
bmp.Save("@image.png", ImageFormat.Png);
Image img = new Bitmap("@image.png");
Rectangle source = new Rectangle(0, 0, ww, hh);
Image cropped = CropImage(img, source);
cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png"));
}
private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle,Rectangle? destinationRectangle = null)
{
if (destinationRectangle == null)
{
destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size);
}
var croppedImage = new Bitmap(destinationRectangle.Value.Width,
destinationRectangle.Value.Height);
using (var graphics = Graphics.FromImage(croppedImage))
{
graphics.DrawImage(originalImage, destinationRectangle.Value,
sourceRectangle, GraphicsUnit.Pixel);
}
return croppedImage;
}
从评论中我了解到您想在将原始图像保存到磁盘之前裁剪图像。在这种情况下,您可以直接将图像提供给裁剪方法:
// bmp.Save("@image.png", ImageFormat.Png);
// Image img = new Bitmap("@image.png");
Rectangle source = new Rectangle(0, 0, ww, hh);
Image cropped = CropImage(bmp, source); // <-- bmp supplied to crop method directly
cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png"));
我有一张照片(这张照片有 X:467 Y:300)我只需要从 X:0 Y:0 到 X:200 Y:45 我使用此代码进行裁剪,但我丢失了 X:0 Y:0 到 X:200 Y:45 的数据,我不知道该怎么做
bmp.Save("@image.png", ImageFormat.Png);
Image img = new Bitmap("@image.png");
Rectangle source = new Rectangle(0, 0, ww, hh);
Image cropped = CropImage(img, source);
cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png"));
}
private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle,Rectangle? destinationRectangle = null)
{
if (destinationRectangle == null)
{
destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size);
}
var croppedImage = new Bitmap(destinationRectangle.Value.Width,
destinationRectangle.Value.Height);
using (var graphics = Graphics.FromImage(croppedImage))
{
graphics.DrawImage(originalImage, destinationRectangle.Value,
sourceRectangle, GraphicsUnit.Pixel);
}
return croppedImage;
}
从评论中我了解到您想在将原始图像保存到磁盘之前裁剪图像。在这种情况下,您可以直接将图像提供给裁剪方法:
// bmp.Save("@image.png", ImageFormat.Png);
// Image img = new Bitmap("@image.png");
Rectangle source = new Rectangle(0, 0, ww, hh);
Image cropped = CropImage(bmp, source); // <-- bmp supplied to crop method directly
cropped.Save(Path.GetDirectoryName("@image.png") + "croppped" + Path.GetExtension("@image.png"));