C # bitmap object, color looks transparent

I am working on a C # program that takes screenshots of a potion on a user's screen. For most users, it works as it should, but I recently ran into one problem. It seems that (at least) one pixel color that always looks transparent in the output image. Any instance of color # 0D0B0C (RGB 13, 11, 12) looks transparent in the saved png. This is with PixelFormat set to Format32bppArgb. If I set it to Format32bppRgb or Format24bppRgb format, the same pixel color will appear in black in the saved png.

I have no idea what could be causing this, but the only thing I could do to “fix” it was to clear the graphic object to that color before doing CopyFromScreen (). I do not want to do this, although for several reasons. Firstly, I don’t know if this is the only color that has a problem (there are a lot of possibilities with 16,777,216 colors), and secondly, I hate hack fixes, it looks like a hack fix.

Can anyone shed some light on what might cause this problem? I got confused with PixelFormat in creating a bitmap and using the CopyPixelOperation method in the CopyFromScreen method nothing works. The fact that clearing the graphic object from this color “corrects” seems to tell me that the transparency comes from the screen data itself, but that doesn't make sense. I have been looking at this for too long, I think I need a new perspective. If anyone knows why this could happen, I'd love to hear it. Thanks.

+6
source share
4 answers

Is alpha value 0 possible? Have you checked it?

Because the big difference between Format32bppArgb and Format32bppRgb is that the second format does not know the alpha channel.

+1
source

Had the same problem when rendering a control into a bitmap. I managed to fix this by creating another bitmap with PixelFormat.Format32bppRgb and BitBlt'ing it to him. Hope this helps!

public class ScreenCapture { [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); /// <summary> /// Returns an image of the control /// </summary> /// <param name="control">The control object whose image is wanted</param> /// <returns>Image of the control</returns> /// <remarks>This is based on code from /// http://www.dotnet247.com/247reference/a.aspx?u=http://www.c-sharpcorner.com/Code/2002/April/ScreenCaptureUtility.asp /// with changes made to prevent 0D0B0C transparency issues</remarks> public static Image GetControlImage(Control control) { Graphics g1 = control.CreateGraphics(); // Create a bitmap the same size as the control Image MyImage = new Bitmap(control.ClientRectangle.Width, control.ClientRectangle.Height, PixelFormat.Format32bppRgb); (MyImage as Bitmap).SetResolution(g1.DpiX, g1.DpiY); Graphics g2 = Graphics.FromImage(MyImage); IntPtr dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); // BitBlt from one DC to the other BitBlt(dc2, 0, 0, control.ClientRectangle.Width, control.ClientRectangle.Height, dc1, 0, 0, 13369376); // Release Device Contexts g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); // This statement runs the garbage collector manually // (If not present, uses up large amounts of memory...) GC.Collect(); return MyImage; } } 
0
source

I just had to request CopyFromScreen to a bitmap that has no alpha channel at all, for example:

 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 it has transparent pixel holes with Format32bppArgb but not with Format32bppRgb

0
source

It looks like you have a bad pixel on your screen.

-2
source

Source: https://habr.com/ru/post/910227/


All Articles