Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:49:33+00:00 2026-05-26T15:49:33+00:00

Here is the test i wrote and that will currently fail: var unusableColor =

  • 0

Here is the test i wrote and that will currently fail:

var unusableColor = Color.FromArgb(13, 19, 20, 19);
var retrievedColor = Color.Empty;
var tempFile = Path.GetTempFileName();

using (var bitmap = new Bitmap(1, 1))
{
    bitmap.SetPixel(0, 0, unusableColor);
    bitmap.Save(tempFile, ImageFormat.Png);
}

using (var image = Image.FromFile(tempFile))
// This will lead to the error
using (var bitmap = new Bitmap(image))
// But this will work
//using (var bitmap = (Bitmap)image)
{
    retrievedColor = bitmap.GetPixel(0, 0);
}

Assert.That(retrievedColor, Is.SameAs(unusableColor));

If you take a look into the retrievedColor you’ll find that it will be the same as Color.FromArgb(13, 19, 19, 19). So the difference will be that the green part has changed from 20 to 19.

Any idea why this happens or under which circumstances the constructor of the Bitmap will change a pixel?

Update

Seems to be a deeper nested problem. By replacing the Bitmap constructor by a simple cast of the image variable the problem goes away. This maybe solves the problem, but it doesn’t explain it. Further more i was able to reproduce the problem even in Paint.Net by the following procedure:

  • Open Paint.Net and create a new image (size doesn’t matter)
  • Select all (Ctrl+A)
  • Remove the selection (Del)
  • Open the color dialog (F8)
  • Enter the above values for RGB (19, 20, 19) and at the bottom the transparency (13).
  • Select the fill tool (F)
  • Fill the color into the empty image
  • Select the color selection tool (K)
  • Click somewhere into your fresh image and watch the color dialog

So it seems it is maybe a deeper problem, not caused by the Bitmap or Image class but maybe by some deeper functionality like GDI+ or something similar.

Update 2

I just wrote a new test to find out all affected colors:

for (int a = 0; a < 256; a++)
{
    for (int r = 0; r < 256; r++)
    {
        for (int g = 0; g < 256; g++)
        {
            for (int b = 0; b < 256; b++)
            {
                using (var bitmap = new Bitmap(1, 1))
                {
                    var desiredColor = Color.FromArgb(a, r, g, b);
                    bitmap.SetPixel(0, 0, desiredColor);

                    // This will fail in a lot of colors with a low alpha channel value
                    using (var copiedBitmap = new Bitmap(bitmap))
                    // This will work, cause the information is entirely copied.
                    //using (var copiedBitmap = (Bitmap)bitmap.Clone())
                    {
                        var retrievedColor = copiedBitmap.GetPixel(0, 0);

                        if (desiredColor != retrievedColor)
                        {
                            Debug.Print(desiredColor + " != " + retrievedColor);
                        }
                    }
                }
            }
        }
    }

Please don’t let it run completely on itself, cause it will take a loonng time to finish and it also finds a looots of differences. But what you can see, if you play around with the transparency (setting to 1 or 10) then you’ll see that the RGB values use this as some kind of bit depth.

So the problem occurs if you create a new Bitmap from an existing one that uses low transparency values. The real root cause seems to be far down in GDI, Kernel or somewhere in this area and can’t be solved from .Net.

Simply be aware that a color can change by calling the bitmap constructor if the color has a low transparency value. If you really need the original colors to stay alive in a second instance instead use (Bitmap)myBitmap.Clone() or if you load it from disk use (Bitmap)Image.FromFile(filename) cause Image is only an abstract class which will normally instantiated through the Bitmap class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:49:34+00:00Added an answer on May 26, 2026 at 3:49 pm

    I checked PNG file saved with your code using Paint.NET and pixel color is exactly unusableColor.
    If you change your reading code with this:

    using (Bitmap bitmap = (Bitmap)Image.FromFile(tempFile))
    {
        retrievedColor = bitmap.GetPixel(0, 0);
    }
    

    everything works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often hear around here from test driven development people that having a function
Here is a test description, testing the Create New Widget use-case. Confirm that you
here is the test page http://www.studioteknik.com/html/test-portfolio.html I got no error, but no hover-slide effect...
Here is a test framework to show what I am doing: create a new
Here is my test page (dont mind the layout right now) https://www.bcidaho.com/test_kalyani/employer-plans-test.asp i found
http://steph.net23.net/work.php here is my test link. This page has a jquery script in it
I've got 256 colors working great in my terminal ( test scripts here ),
I've got a bunch of strings like: Hello, here's a test colon&#58;. Here's a
I'm having a problem with Python's subprocess.Popen method. Here's a test script which demonstrates
Apparently, NSFileManager is unable to delete files created by mkstemp() . Here's some test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.