---
title: "LosslessJpegTransform..::.WriteCropped Method (String, Rectangle)"
canonical: https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteCropped_2_45371d77.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.WriteCropped Method (String, Rectangle)
 
Crops a portion of the uncompressed JPEG image and writes it to specified file.
 
**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 WriteCropped ( _fileName As String, _rectangle As Rectangle _
)
```
 
```csharp
public void WriteCropped(string fileName,Rectangle rectangle
)
```
 
### Parameters
 
 - fileName
     - Type: [System.String](http://msdn2.microsoft.com/en-us/s1wwdcbf)
The name of the file where you want to save the result.

 - rectangle
     - Type: [System.Drawing.Rectangle](http://msdn2.microsoft.com/en-us/1zk39146)
 [Rectangle](http://msdn2.microsoft.com/en-us/1zk39146) 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](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 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)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_AlignToSampleSize_2_196b06b9.htm.md) 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

- [LosslessJpegTransform Class](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform.htm.md)
- [WriteCropped Overload](https://www.graphicsmill.com/docs/gm5/O_T_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteCropped.htm.md)
- [Aurigma.GraphicsMill.Codecs Namespace](https://www.graphicsmill.com/docs/gm5/N_Aurigma_GraphicsMill_Codecs.htm.md)
- [LosslessJpegTransform.AlignToSampleSize(Rectangle, JpegAlignToSampleSizeMode)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_AlignToSampleSize_2_196b06b9.htm.md)
- [LosslessJpegTransform.Write(String)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_Write_1_bb3a7a4f.htm.md)
- [LosslessJpegTransform.WritePatched(String, Point, Bitmap)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WritePatched_3_f5f3fae2.htm.md)
- [LosslessJpegTransform.WriteRotated(String, RotateFlipType)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteRotated_2_ed07628a.htm.md)
- [LosslessJpegTransform.WriteTransposed(String)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteTransposed_1_bb3a7a4f.htm.md)
- [LosslessJpegTransform.WriteTransversed(String)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_LosslessJpegTransform_WriteTransversed_1_bb3a7a4f.htm.md)
