Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Drawing.Imaging.ImageAttributes.SetColorKey() is set but not applied. #8806

Closed
KostaVlev opened this issue Jul 16, 2017 · 3 comments
Closed
Labels
area-System.Drawing System.Drawing issues

Comments

@KostaVlev
Copy link
Contributor

I am writing test for System.Drawing.Imaging.ImageAttributes dotnet/runtime#22130:

private readonly Rectangle _rectangle = new Rectangle(0, 0, 64, 64);

[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void SetColorKey_Success()
{
    using (var bitmap = new Bitmap(_rectangle.Width, _rectangle.Height))
    using (var graphics = Graphics.FromImage(bitmap))
    using (var imageAttr = new ImageAttributes())
    {
        imageAttr.SetColorKey(Color.FromArgb(50, 50, 50), Color.FromArgb(150, 150, 150));

        bitmap.SetPixel(0, 0, Color.FromArgb(255, 100, 100, 100));
        graphics.DrawImage(bitmap, _rectangle, _rectangle.X, _rectangle.Y, _rectangle.Width, _rectangle.Height, GraphicsUnit.Pixel, imageAttr);
        Assert.Equal(Color.FromArgb(0, 0, 0, 0), bitmap.GetPixel(0, 0));
    }
}

Expected: Color.FromArgb(0, 0, 0, 0)
Actual: Color.FromArgb(255, 100, 100, 100)

I am running the test on netfx and netcoreapp and it fails on both.

Looks like transparency isn't get applied.

[EDIT] Fixed code formatting - @karelz

@karelz
Copy link
Member

karelz commented Jul 16, 2017

FYI: @hughbe @mellinoe

@hughbe
Copy link
Contributor

hughbe commented Jul 16, 2017

From https://msdn.microsoft.com/en-us/library/windows/desktop/ms535429(v=vs.85).aspx sample code:

Ignoring the possibility i may be doing this C++ incorrectly, it looks as though this is a bug in GDI+.

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if (size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if (pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for (UINT j = 0; j < num; ++j)
	{
		if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}
	}

	free(pImageCodecInfo);
	return -1;  // Failure
}

static void test_imageAttributes() {
	Image image{L"C:\\Users\\hugh\\Documents\\Visual Studio 2017\\Projects\\ConsoleApplication1\\ConsoleApplication1\\173x183_indexed_8bit.bmp"};
	Graphics graphics((HWND)0);

	ImageAttributes imAtt;
	imAtt.SetColorKey(
		Color(0, 0, 0),
		Color(255, 255, 255),
		ColorAdjustTypeBitmap);

	CLSID pngClsid;
	GetEncoderClsid(L"image/png", &pngClsid);

	graphics.DrawImage(
		&image,
		Rect(20, 20, image.GetWidth(), image.GetHeight()),  // dest rect
		0, 0, image.GetWidth(), image.GetHeight(),          // source rect
		UnitPixel,
		&imAtt);

	image.Save(L"C:\\Users\\hugh\\Desktop\\stuff.png", &pngClsid);
}

In the sample code above, modified from the sample online, it shows that the C++ implementation looks broken.
I've sent @mellinoe an email with a GDI+ bug before and he wasn't sure the library has a maintainer. He also wasn't sure of the proper channel to route bugs in GDI+.

If this is indeed a GDI+ bug, I'm really not sure there's much we can do here. I think we should definitely keep the test, but maybe disable it.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged The team needs to look at this issue in the next triage label Feb 23, 2020
@tannergooding tannergooding removed the untriaged The team needs to look at this issue in the next triage label Jul 8, 2020
@JeremyKuhne JeremyKuhne transferred this issue from dotnet/runtime Mar 14, 2023
@JeremyKuhne JeremyKuhne added the area-System.Drawing System.Drawing issues label Mar 14, 2023
@JeremyKuhne
Copy link
Member

As this appears to be a GDI+ issue there isn't much we can do here.

@ghost ghost removed this from the Future milestone Mar 14, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Drawing System.Drawing issues
Projects
None yet
Development

No branches or pull requests

7 participants