---
title: "LosslessJpegTransform..::.WritePatched Method (Stream, Point, Bitmap)"
canonical: https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WritePatched_3_8cd42e51.htm
product: "Graphics Mill 5.5 for .NET"
---

This documentation is for the old version. [Go to the latest Graphics Mill docs](https://www.graphicsmill.com/docs/gm/)

# LosslessJpegTransform.WritePatched Method (Stream, Point, Bitmap)
 
Compresses given bitmap to JPEG, overwrites a portion of JPEG data of the [LosslessJpegTransform](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform.htm.md) by this bitmap, and writes the result into the specified stream.
 
**Namespace:** [Aurigma.GraphicsMill.Codecs](https://www.graphicsmill.com/docs/gm5/N_Aurigma_GraphicsMill_Codecs.htm.md)
 **Assembly:** Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)
 
## Syntax
  
```
Public Sub WritePatched ( _stream As Stream, _offset As Point, _bitmap As Bitmap _
)
```
 
```csharp
public void WritePatched(Stream stream,Point offset,Bitmap bitmap
)
```
 
### Parameters
 
 - stream
     - Type: [System.IO.Stream](http://msdn2.microsoft.com/en-us/8f86tw9e)
The stream where you want to save the result.

 - offset
     - Type: [System.Drawing.Point](http://msdn2.microsoft.com/en-us/bk9hwzbw)
 [Point](http://msdn2.microsoft.com/en-us/bk9hwzbw) 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](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Bitmap.htm.md)
An instance of the [Bitmap](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Bitmap.htm.md) 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](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform.htm.md) and open it using the [Open(String)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_Open_1_bb3a7a4f.htm.md) method.
3. Use [AlignToSampleSize(Point, Bitmap)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_AlignToSampleSize_2_53be7bca.htm.md) to align the rectangle specifying the image part to JPEG sample size (see below).
4. Use the [WriteCropped(Stream, Rectangle)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteCropped_2_b9ff8166.htm.md) to get a portion of JPEG without recompression.
5. Create a new [Bitmap](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Bitmap.htm.md) instance and load the resulting JPEG file into it (e.g. using the [Load(String)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Bitmap_Load_1_bb3a7a4f.htm.md) method).
6. Apply the necessary effect to the bitmap (e.g. watermark, red eye removal, etc).
7. Use the [WritePatched(String, Point, Bitmap)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WritePatched_3_f5f3fae2.htm.md) 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](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform.htm.md) with the [Close()](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_Close.htm.md) 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)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_AlignToSampleSize_2_53be7bca.htm.md) 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

- [LosslessJpegTransform Class](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform.htm.md)
- [WritePatched Overload](https://www.graphicsmill.com/docs/gm5/O_T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WritePatched.htm.md)
- [Aurigma.GraphicsMill.Codecs Namespace](https://www.graphicsmill.com/docs/gm5/N_Aurigma_GraphicsMill_Codecs.htm.md)
- [LosslessJpegTransform.AlignToSampleSize(Point, Bitmap)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_AlignToSampleSize_2_53be7bca.htm.md)
- [LosslessJpegTransform.Write(Stream)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_Write_1_bca7c9fe.htm.md)
- [LosslessJpegTransform.WriteCropped(Stream, Rectangle)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteCropped_2_b9ff8166.htm.md)
- [LosslessJpegTransform.WriteRotated(Stream, RotateFlipType)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteRotated_2_38dd4041.htm.md)
- [LosslessJpegTransform.WriteTransposed(Stream)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteTransposed_1_bca7c9fe.htm.md)
- [LosslessJpegTransform.WriteTransversed(Stream)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteTransversed_1_bca7c9fe.htm.md)
