---
title: "Recompress JPEG partially"
canonical: https://www.graphicsmill.com/samples/Codecs/Jpeg/RecompressJpegPartially_cs
product: "Graphics Mill"
source: "Codecs/Jpeg/RecompressJpegPartially.cs"
---

# Recompress JPEG partially

Applies mosaic effect on region of image with partial JPEG recompression.

## Instruction

Write a C# program that applies a mosaic effect to a specific region of a JPEG image and updates only that region using partial JPEG recompression. Use the memory-friendly Pipeline API to generate the patch bitmap.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
var rect = new System.Drawing.Rectangle(264, 192, 264, 184);

using (var patchBitmap = new Bitmap())
{
    // Apply crop and mosaic transfroms
    using (var reader = ImageReader.Create("Chicago.jpg"))
    using (var crop = new Crop(rect))
    using (var mosaic = new Mosaic(10, 10))
    {
        Pipeline.Run(reader + crop + mosaic + patchBitmap);
    }

    // Patch JPEG
    using (var losslessJpeg = new LosslessJpeg("Chicago.jpg"))
    {
        rect = losslessJpeg.AlignToMCUSize(rect, JpegAlignToSampleSizeMode.Patch);
        losslessJpeg.WritePatched("ResompressJpegPartially.jpg", rect.Location, patchBitmap);
    }
}
```

## Explanation

Here is a C# program that applies a mosaic filter to a rectangular region of a JPEG image and saves the result using partial JPEG recompression:

This code defines a rectangle, processes the corresponding image patch with a mosaic effect using the memory-friendly Pipeline API, and writes the result back into the original JPEG using `LosslessJpeg.WritePatched()`.

## Tags

- [Filesystem](<https://www.graphicsmill.com/samples/tags/filesystem_md>): Loading and saving images from files
- [JPG](<https://www.graphicsmill.com/samples/tags/jpg_md>): Reading and writing JPEG images
- [Lossless JPEG](<https://www.graphicsmill.com/samples/tags/lossless_md>): Applying lossless JPEG operations
- [Pipeline](<https://www.graphicsmill.com/samples/tags/pipeline_md>): Working with images via the Pipeline API
- [Read](<https://www.graphicsmill.com/samples/tags/read_md>): Reading images with codecs
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [Chicago.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Chicago_jpg>) (preview)

## Output

- [ResompressJpegPartially.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/ResompressJpegPartially_jpg>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ImageReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageReader_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Codecs.JpegAlignToSampleSizeMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegAlignToSampleSizeMode.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_LosslessJpeg.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg.AlignToMCUSize(Rectangle,JpegAlignToSampleSizeMode)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_LosslessJpeg_AlignToMCUSize_2_196b06b9.htm.md>)
- [Aurigma.GraphicsMill.Codecs.LosslessJpeg.WritePatched(String,Point,Bitmap)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_LosslessJpeg_WritePatched_3_f5f3fae2.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Crop](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Crop.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Mosaic](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Mosaic.htm.md>)
