Crop Image Issue when the actual image is not displayed using C #

I use JCrop to crop Image . It works fine if I show an Actual Image to a user. But if I show Resize Image , not Actual Image , I get Co-ordinates from Resize Image . Then, how to draw an Image based on this? Here I go the way Image Saved Image .

In short, If Saved Image size, if for ie 715 * 350 , then I show it in a CSS-based Small Size popup. So, I get the Co-ordinates this Small Size Image . and I apply those Co-ordinates on the Main Image .

My code is:

 using (System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Img)) { using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height)) { bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution); using (System.Drawing.Graphics Graphic = System.Drawing.Graphics.FromImage(bmp)) { Graphic.SmoothingMode = SmoothingMode.AntiAlias; Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; Graphic.DrawImage(OriginalImage, new System.Drawing.Rectangle(0, 0, Width, Height), X, Y, Width, Height, System.Drawing.GraphicsUnit.Pixel); MemoryStream ms = new MemoryStream(); bmp.Save(ms, OriginalImage.RawFormat); ms.Close(); ms.Flush(); ms.Dispose(); return ms.GetBuffer(); } } } 
+6
source share
2 answers

The code you are showing is for resizing, not cropping (in the call to Graphic.DrawImage() you don’t need the cropping coordinates and just use the width / height of the destination)

To crop the image, you can simply use the Bitmap.Clone() method. Just pass it the cropping coordinates that you extracted from JCrop . ( cropzone in the following example)

 public static async Task CropImage() { var client = new WebClient(); var sourceimg = new Uri(@"http://logonoid.com/images/stack-overflow-logo.png"); var destination = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "logoCropped.png")); if (destination.Exists) destination.Delete(); using (Stream sourceStream = await client.OpenReadTaskAsync(sourceimg)) { using (Bitmap source = new Bitmap(sourceStream)) { Rectangle cropzone = new Rectangle(0, 0, 256, 256); using (Bitmap croppedBitmap = source.Clone(cropzone, source.PixelFormat)) { croppedBitmap.Save(destination.FullName, ImageFormat.Png); } } } } 

Some tips about your code:

  • With simple cropping, there is no resize operation. Therefore, the parameters SmoothingMode , InterpolationMode , PixelOffsetMode useless.
  • About MemoryStream , it's best to use it in a using statement. It avoids the manual calls of Close() and Dispose() , and ensures that they are called no matter what happens. And about the Flush() method, it just does nothing in the MemoryStream class.
+5
source

Jcrop has the tureSize property.

 $.Jcrop('#image',{ trueSize: [715, 350] }); 

You should get the correct coordinates for the large image.

http://deepliquid.com/content/Jcrop_Sizing_Issues.html

+1
source

Source: https://habr.com/ru/post/982480/


All Articles