Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Bitmap object, color appearing as transparent

I'm working on a program in C# that takes screenshots of a potion of the user's screen. For the most pert it works as it should, but I've recently run into one issue. There seems to be (at least) one pixel color that always appears as transparent in the output image. Any instance of the color #0D0B0C (RGB 13, 11, 12) appears transparent in the saved png. This is with the PixelFormat set to Format32bppArgb. If I set it to Format32bppRgb or Format24bppRgb, that same pixel color appears as black in the saved png.

I have no idea what could be causing this, but the only thing I've been able to do to "fix" it is to clear the graphics object to that color before doing CopyFromScreen(). I'm loathe to do that though for a few reasons. First, I don't know if that's the only color that has the issue (what with 16,777,216 colors there's quite a few possibilities), and second, I hate hack fixes, this seems like a hack fix.

Can anyone shed any light on what might be causing this issue? I've messed with the PixelFormat on the bitmap creation and with the CopyPixelOperation in the CopyFromScreen method, nothing seems to work. The fact that clearing the graphics object to that color "fixes" it seems to tell me that the transparency is coming from the screen data itself, but that doesn't make sense. I've been staring at this for too long, I think I need a fresh perspective on it. If anyone has any idea why this might be happening I'd love to hear it. Thank you.

like image 916
HaLo2FrEeEk Avatar asked Mar 08 '12 07:03

HaLo2FrEeEk


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

I simply had to request the CopyFromScreen into a bitmap that does NOT have an Alpha channel at all, such as:

Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height,     System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(bounds.Location, new Point(0, 0), bitmap.Size);

I confirmed that this has transparent pixel holes with Format32bppArgb but not with Format32bppRgb

like image 192
Dave Avatar answered Oct 20 '22 13:10

Dave