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 6939031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:34:01+00:00 2026-05-27T12:34:01+00:00

In C#, .NET 2.0, Windows Forms, Visual Studio Express 2010, I’m saving an image

  • 0

In C#, .NET 2.0, Windows Forms, Visual Studio Express 2010, I’m saving an image made of the same color:

  Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  using (Graphics graphics = Graphics.FromImage(bitmap))
  {
      Brush brush = new SolidBrush(color);
      graphics.FillRectangle(brush, 0, 0, width, height);
      brush.Dispose();
  }

  bitmap.Save("test.png");
  bitmap.Save("test.bmp");

If I’m using, for example

Color [A=153, R=193, G=204, B=17] or #C1CC11

after I’m saving the image and open it in an external viewer such as Paint.NET, IrfanView, XNView, etc. I am told that the color of the image is actually:

Color [A=153, R=193, G=203, B=16] or #C1CB10

so it’s a similar color, but not the same!

I tried with both PNG and BMP saving.

When transparency (alpha) is involved, .NET saves a different color!
When the alpha is 255 (no transparency), it saves the corrent color.

  • 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-27T12:34:01+00:00Added an answer on May 27, 2026 at 12:34 pm

    Thank you, Joe and Hans Passant for your comments.

    Yes, as Joe said, the problem is on the line:

    graphics.FillRectangle(brush, 0, 0, width, height);
    

    Here GDI+ modifies the color with a similar color, but not the exact one.

    It seems that the solution is to write the color values directly in the pixels, using Bitmap.LockBits and Marshal.Copy:

            Bitmap bitmap = new Bitmap(this.currentSampleWidth, this.currentSampleHeight, PixelFormat.Format32bppArgb);
    
            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
    
            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;
    
            // Declare an array to hold the bytes of the bitmap (32 bits per pixel)
            int pixelsCount = bitmap.Width * bitmap.Height;
            int[] argbValues = new int[pixelsCount];
    
            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, pixelsCount);
    
            // Set the color value for each pixel.
            for (int counter = 0; counter < argbValues.Length; counter++)
                argbValues[counter] = color.ToArgb();
    
            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, pixelsCount);
    
            // Unlock the bits.
            bitmap.UnlockBits(bmpData);
    
            return bitmap;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Windows Vista and Visual Studio 2010. Create a .Net 4 Windows Forms
I created a VB.NET Windows Forms Application in Visual Studio 2008. When I run
I'm working on a visual studio 2005 vb.net windows forms project that's been around
Using Visual Studio 2008, c#, .net 2.0. I have a Windows Forms client application
I'm using Visual Studio 2008, .net Framework 3.5 for a Windows forms client-server app
Can someone explain this to me? In Visual Studio 2010, create a VB.net Windows
I have a simple Windows Forms (C#, .NET 2.0) application, built with Visual Studio
In Windows Forms (.NET 2.0, Visual Studio 2005 SP1) : I have a typed
I'm developing a Windows Forms Application in Visual Basic .NET with Visual Studio 2008.
Working with C# in Visual Studio 2008 (.NET 3.5). Looking into System.Windows.Forms.MouseEventArgs . I'm

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.