---
title: "Place JPEG photos into a PDF without recompression"
canonical: https://www.graphicsmill.com/samples/Codecs/Pdf/PlaceJpegWithoutRecompression_cs
product: "Graphics Mill"
source: "Codecs/Pdf/PlaceJpegWithoutRecompression.cs"
---

# Place JPEG photos into a PDF without recompression

This sample shows how to put JPEG photos into a PDF quickly and with little memory by storing their original compressed data in the file as is. An image loaded with ` CompressedImage.CreateFromFile ` keeps its encoded bytes, and ` Graphics.DrawImage ` writes them to the PDF without decoding and compressing them again: the photos are never held in memory as pixels, no time is spent on JPEG decoding and encoding, and there is no second JPEG quality loss. RGB and CMYK JPEG files are both kept as is. For comparison, the sample writes the same photos to a JPEG-compressed PDF once more from decoded ` Bitmap ` objects and prints how long each way takes.

## Instruction

Use the PDF API to create a PDF with one page per JPEG photo, keeping the original JPEG data of every photo with CompressedImage.CreateFromFile and Graphics.DrawImage, and compare the time it takes with writing the same photos from decoded bitmaps.

## Usings

```csharp
using System;
using System.Diagnostics;
using Aurigma.GraphicsMill.Codecs;
```

## Code Snippet

```csharp
var photos = new[] { "Venice.jpg", "Copenhagen_CMYK.jpg" };

var stopwatch = Stopwatch.StartNew();

using (var writer = new PdfWriter("PlaceJpegWithoutRecompression.pdf"))
using (var graphics = writer.GetGraphics())
{
    writer.Compression = CompressionType.Jpeg;

    foreach (var photo in photos)
    {
        // Keeps the JPEG data as it is in the file instead of decoding it to pixels.
        using (var image = CompressedImage.CreateFromFile(photo))
        {
            writer.AddPage(image.Width, image.Height, image.DpiX, image.DpiY);
            graphics.DrawImage(image, 0, 0);
        }
    }
}

Console.WriteLine("CompressedImage: {0} ms", stopwatch.ElapsedMilliseconds);

// The same photos decoded to bitmaps: every pixel is held in memory and encoded to JPEG again.
stopwatch.Restart();

using (var writer = new PdfWriter("PlaceJpegDecoded.pdf"))
using (var graphics = writer.GetGraphics())
{
    writer.Compression = CompressionType.Jpeg;

    foreach (var photo in photos)
    {
        using (var bitmap = new Bitmap(photo))
        {
            writer.AddPage(bitmap.Width, bitmap.Height, bitmap.DpiX, bitmap.DpiY);
            graphics.DrawImage(bitmap, 0, 0);
        }
    }
}

Console.WriteLine("Bitmap:          {0} ms", stopwatch.ElapsedMilliseconds);
```

## Explanation

The code:

- Starts a ` Stopwatch `, creates a ` PdfWriter ` for "PlaceJpegWithoutRecompression.pdf" with ` PdfWriter.Compression ` set to ` CompressionType.Jpeg `, and gets its ` Graphics `.
- For every photo, loads it with ` CompressedImage.CreateFromFile `, which keeps the encoded JPEG data (RGB or CMYK) instead of decoding it.
- Adds a page of the photo's size and resolution with ` PdfWriter.AddPage ` and draws the photo with ` Graphics.DrawImage `; the JPEG data goes into the PDF unchanged.
- Writes the same photos with the same settings to "PlaceJpegDecoded.pdf" from ` Bitmap ` objects: every photo is decoded to pixels and the writer encodes it to JPEG again.
- Prints the time each way took.

## Tags

- [Compression](<https://www.graphicsmill.com/samples/tags/compression_md>): Tuning image compression settings
- [JPG](<https://www.graphicsmill.com/samples/tags/jpg_md>): Reading and writing JPEG images
- [PDF](<https://www.graphicsmill.com/samples/tags/pdf_md>): Importing and exporting PDF documents
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [Venice.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Venice_jpg>) (preview)
- [Copenhagen\_CMYK.jpg](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/Copenhagen_CMYK_jpg>) (preview)

## Output

- [PlaceJpegWithoutRecompression.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PlaceJpegWithoutRecompression_pdf>) (download)
- [PlaceJpegWithoutRecompression.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PlaceJpegWithoutRecompression_png>) (preview)
- [PlaceJpegWithoutRecompression.txt](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/PlaceJpegWithoutRecompression_txt>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Codecs.CompressionType](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_CompressionType.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.Compression](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfWriter_Compression.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_CompressedImage.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage.CreateFromFile(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_CompressedImage_CreateFromFile_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_CompressedImage_DpiX.htm.md>)
- [Aurigma.GraphicsMill.CompressedImage.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_CompressedImage_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawImage(Bitmap,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawImage_3_b9413d4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawImage(Pipeline,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawImage_3_81ae131e.htm.md>)
