I am doing sort of a limited graphics editor in a desktop applic开发者_如何学运维ation in c# 3.5 GDI. the user first selects an image which is shown in a picturebox control which is smaller in size so image resizing is done to fit the picture.
For cropping, the user selects the area to crop. there are a number of example on the net that explains how to crop the image but none of them explains the case when the area is selected on a thumbnail but the cropping is done on the original image i.e. some kind of mapping is done between the two images.
all the graphic editor provide similar functionality. can you direct me to a link which explains how to do this?
Sounds to me like you need to calculate the crop rectangle on the original image yourself based on the relative sizes of the picture and the thumbnail.
public static class CoordinateTransformationHelper
{
public static Point ThumbToOriginal(this Point point, Size thumb, Size source)
{
Point rc = new Point();
rc.X = (int)((double)point.X / thumb.Width * source.Width);
rc.Y = (int)((double)point.Y / thumb.Height * source.Height);
return rc;
}
public static Size ThumbToOriginal(this Size size, Size thumb, Size source)
{
Point pt = new Point(size);
Size rc = new Size(pt.ThumbToOriginal(thumb, source));
return rc;
}
public static Rectangle ThumbToOriginal(this Rectangle rect, Size thumb, Size source)
{
Rectangle rc = new Rectangle();
rc.Location = rect.Location.ThumbToOriginal(thumb, source);
rc.Size = rect.Size.ThumbToOriginal(thumb, source);
return rc;
}
}
Usage example:
Size thumb = new Size(10, 10);
Size source = new Size(100, 100);
Console.WriteLine(new Point(4, 4).ThumbToOriginal(thumb, source));
Console.WriteLine(new Rectangle(4, 4, 5, 5).ThumbToOriginal(thumb, source));
here's a really easy method to crop a System.Drawing.Image
public static Image CropImage(Image image, Rectangle area)
{
Image cropped = null;
using (Bitmap i = new Bitmap(image))
using (Bitmap c = i.Clone(area, i.PixelFormat))
cropped = (Image)c;
return cropped;
}
pass in an Image and the area that you want to crop and that should do it
精彩评论