This documentation is for the old version. Go to the latest Graphics Mill docs

LosslessJpegTransform.WriteCropped Method (String, Rectangle)

Crops a portion of the uncompressed JPEG image and writes it to specified file.

Namespace: Aurigma.GraphicsMill.Codecs
Assembly: Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)

Syntax

Visual Basic
Public Sub WriteCropped ( _
	fileName As String, _
	rectangle As Rectangle _
)
C#
public void WriteCropped(
	string fileName,
	Rectangle rectangle
)

Parameters

fileName

Type: System.String

The name of the file where you want to save the result.
rectangle

Type: System.Drawing.Rectangle

Rectangle object which specifies a portion of the image you need to crop. The rectangle must be inside the image. Note, coordinates of this rectangle and coordinates of the cropped portion of image may be different (see Remarks section for more details).

Remarks

You can use this method to recompress only a part of an image. For example, let's assume you want to apply a watermark to an image or remove an effect of red eyes. It obviously makes sense to recompress only that part of image which you modify without recompressing the other parts. It can be easily achieved with the following algorithm:

  1. Determine what part of image you need to change. You may need to load it into a bitmap so that the user could select this part manually, or calculate it somehow else.
  2. Create LosslessJpegTransform and open it using the Open(String) method.
  3. Use AlignToSampleSize(Point, Bitmap) to align the rectangle specifying the image part to JPEG sample size (see below).
  4. Use the WriteCropped(Stream, Rectangle) to get a portion of JPEG without recompression.
  5. Create a new Bitmap instance and load the resulting JPEG file into it (e.g. using the Load(String) method).
  6. Apply the necessary effect to the bitmap (e.g. watermark, red eye removal, etc).
  7. Use the WritePatched(String, Point, Bitmap) method to store the resulting bitmap into a JPEG file. Graphics Mill for .NET will automatically compress this bitmap with the same compression options as the original JPEG file and overwrite the appropriate portion of compressed JPEG data.
  8. Do not forget to close the LosslessJpegTransform with the Close() method.
Note

Due to specifics of JPEG, you cannot specify arbitrary coordinates to crop. Its top-left corner should be aligned on the size of JPEG sample data. Typically it is a number divisible by 8. To calculate the actual rectangle, you can use the AlignToSampleSize(Rectangle, JpegAlignToSampleSizeMode) method. You just pass your rectangle to this method and it returns the nearest rectangle (with modified position aligned to the JPEG sample size).

If you pass a rectangle which is not aligned, Graphics Mill for .NET automatically aligns it. As a result, coordinates of the left-top corner of this rectangle will be rounded down by the JPEG sample size but coordinates of the right-bottom corner will not be changed. For example, if you specify this rectangle (11, 11, 20, 20) it will be aligned to (8, 8, 23, 23) if the JPEG sample size is 8x8.

See Also

Reference