Skip to content

PDFsharp 6.1.0 and 6.1.1 incorrectly duplicate first image

ThomasHoevel edited this page Dec 12, 2024 · 2 revisions

Issue with GDI build 6.1.0

When images are loaded from a MemoryStream, the first image may be displayed instead of the proper image loaded later. This problem is specific for the GDI build and was fixed with PDFsharp 6.1.1.

Issue with Core build 6.1.0 and 6.1.1

When images are loaded where differences only exist in the transparency mask, the first image may be displayed instead of the proper image loaded later. This problem is specific to the GDI build and will be fixed with the next release of PDFsharp.

Workarounds for this issue:

  • Make sure the image bits are different - it is enough if image height or image width is different
  • Use GDI or WPF build instead of Core build, if possible, until fixed Core build is published
  • Download the PDFsharp repository with version 6.1.1 and make the changes shown below

To fix the issue in the source code of PDFsharp 6.1.1, locate this code block:

else if (iid is ImageDataBitmap bmp)
{
    var md5 = System.Security.Cryptography.MD5.Create();
    var hash = md5.ComputeHash(bmp.Data, 0, bmp.Length);
    image._path = "*md5:" + HashToString(hash);
}

Replace it with this code block:

else if (iid is ImageDataBitmap bmp)
{
    var md5 = System.Security.Cryptography.MD5.Create();
    var hash = md5.ComputeHash(bmp.Data, 0, bmp.Length);
    if (bmp.AlphaMask != null! && bmp.AlphaMaskLength > 0)
    {
        var hash2 = md5.ComputeHash(bmp.AlphaMask, 0, bmp.AlphaMaskLength);
        image._path = "*md5:" + HashToString(hash) + HashToString(hash2);
    }
    else if (bmp.BitmapMask != null! && bmp.BitmapMaskLength > 0)
    {
        var hash2 = md5.ComputeHash(bmp.BitmapMask, 0, bmp.BitmapMaskLength);
        image._path = "*md5:" + HashToString(hash) + HashToString(hash2);
    }
    else
    {
        image._path = "*md5:" + HashToString(hash);
    }
}

Both issues are discussed in issue 127.

Issue should be resolved with PDFsharp 6.2.0 Preview 2.