---
title: "Hash From Bitmap"
canonical: https://www.graphicsmill.com/samples/Interoperability/HashFromBitmap/HashFromBitmap_cs
product: "Graphics Mill"
source: "Interoperability/HashFromBitmap/HashFromBitmap.cs"
---

# Hash From Bitmap

Demonstrates computing an MD5 hash from bitmap pixel data via unsafe pointer-based scanline access.

## Instruction

Write a C# program that calculates MD5 hash from pixel data of "Chicago.jpg" by accessing raw bitmap scanlines via unsafe pointer operations and Scan0, copying each scanline to buffer using Marshal.Copy, processing through MD5 TransformBlock incrementally to handle large images without memory overflow, converting indexed formats to 32bpp ARGB first, and formatting hash as uppercase hexadecimal string.

## Usings

```csharp
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
```

## Code Snippet

```csharp
public static void Run()
{
    using (var bitmap = new Bitmap("Chicago.jpg"))
    {
        if (bitmap.PixelFormat.IsIndexed)
        {
            bitmap.ColorManagement.Convert(PixelFormat.Format32bppArgb);
        }

        Console.WriteLine("MD5: {0}", GetMD5Hash(bitmap));
    }
}

private static string GetMD5Hash(Bitmap bitmap)
{
    int scanlineByteWidth = bitmap.Width * bitmap.PixelFormat.Size / 8;

    using (var md5 = MD5.Create())
    {
        unsafe
        {
            byte* pScanline = (byte*)bitmap.Scan0.ToPointer();
            var buffer = new byte[scanlineByteWidth];

            for (int j = 0; j < bitmap.Height; ++j)
            {
                Marshal.Copy((IntPtr)pScanline, buffer, 0, scanlineByteWidth);

                md5.TransformBlock(buffer, 0, scanlineByteWidth, buffer, 0);

                pScanline += bitmap.Stride;
            }

            md5.TransformFinalBlock(new byte[0], 0, 0);
        }

        return string.Concat(Array.ConvertAll(md5.Hash, x => x.ToString("X2")));
    }
}
```

## Explanation

Below is a C# code sample that computes cryptographic hash from bitmap pixel data:

The code demonstrates low-level pixel data hashing using unsafe bitmap access with Scan0 pointer arithmetic, calculating scanline byte width from PixelFormat.Size, and streaming MD5 computation via TransformBlock for memory efficiency on large images. It handles indexed pixel formats by converting to 32bpp ARGB via ColorManagement, processes scanlines sequentially with Marshal.Copy to managed buffer and Stride-aware pointer advancement, and produces standard uppercase hexadecimal MD5 output suitable for image integrity verification, duplicate detection, or content fingerprinting applications.

## Tags

- [Bitmap](<https://www.graphicsmill.com/samples/tags/bitmap_md>): Working with images via the Bitmap API
- [Pixel Access](<https://www.graphicsmill.com/samples/tags/pixel-access_md>): Direct per-pixel bitmap access

## Input

- [Chicago.jpg](<https://www.graphicsmill.com/samples/Interoperability/compiled/Debug/Chicago_jpg>) (preview)

## Output

- [HashFromBitmap.txt](<https://www.graphicsmill.com/samples/Interoperability/compiled/Debug/HashFromBitmap_txt>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.ColorManagement](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_ColorManagement.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.PixelFormat](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_PixelFormat.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.Scan0](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_Scan0.htm.md>)
- [Aurigma.GraphicsMill.Bitmap.Stride](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_Stride.htm.md>)
- [Aurigma.GraphicsMill.ColorManagementProvider.Convert(PixelFormat)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_ColorManagementProvider_Convert_1_3e209c07.htm.md>)
- [Aurigma.GraphicsMill.PixelFormat.IsIndexed](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_PixelFormat_IsIndexed.htm.md>)
- [Aurigma.GraphicsMill.PixelFormat.Size](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_PixelFormat_Size.htm.md>)
