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

LosslessJpegTransform.WritePatched Method (Stream, Point, Bitmap)

Compresses given bitmap to JPEG, overwrites a portion of JPEG data of the LosslessJpegTransform by this bitmap, and writes the result into the specified stream.

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

Syntax

Visual Basic
Public Sub WritePatched ( _
	stream As Stream, _
	offset As Point, _
	bitmap As Bitmap _
)
C#
public void WritePatched(
	Stream stream,
	Point offset,
	Bitmap bitmap
)

Parameters

stream

Type: System.IO.Stream

The stream where you want to save the result.
offset

Type: System.Drawing.Point

Point at which the bitmap should be placed. These coordinates must be inside of image (i.e. x-coordinate must be smaller than width, y-coordinate must be smaller than height). Note, it should be aligned on the JPEG data sample size (see the Remarks section for more details).
bitmap

Type: Aurigma.GraphicsMill.Bitmap

An instance of the Bitmap class which should be placed at the given position.

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 put the bitmap at arbitrary coordinates. They should be aligned on the size of JPEG sample data. Typically it is a number divisible by 8. To calculate aligned coordinates, you can use the AlignToSampleSize(Point, Bitmap) method. You just pass your position and the bitmap to this method and it returns the rectangle where this bitmap will be actually placed (with modified position and dimensions aligned to the JPEG sample size).

If you pass an offset which is not aligned, Graphics Mill for .NET automatically aligns it. As a result x and y coordinates of this offset will be rounded down by the JPEG sample size. For example, if you specify the following point (19, 19) to place the bitmap, it will be actually placed into the (16, 16) position (if the JPEG sample size is 8x8 or 16x16). If the bitmap you pass to this method is not aligned, Graphics Mill for .NET automatically aligns it. As a result its dimension will be enlarged to be divisible by the JPEG sample size and the extended area will be filled with garbage.

See Also

Reference