Getting transparent shell-icons for files and folders in C# - c#

I'm currently working on a small library that enables you to get icons from files and folders. Now, I don't care if it only works on win8+ (cause that's the place I'm going to use it), however, I've run in to a tiny problem with regards to transparency. If you take a look at the following image:
The one I generate (from my library) is to the left, windows explorer is to the right.
Now, as you might see, first off there is 2 black lines in the upper right of the one I generate, second, there is a difference in the background color. So what I'm wondering is this; is there no way to get the exact same image used by windows explorer, or am I simply doing it wrong?
My code (with exception to structs/externs etc. for shortness) bellow, entire code here.
public static class Icon
{
public static Image GetIcon(string fileName, int size)
{
IShellItem shellItem;
Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);
IntPtr hbitmap;
((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);
// get the info about the HBITMAP inside the IPictureDisp
DIBSECTION dibsection = new DIBSECTION();
Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
int width = dibsection.dsBm.bmWidth;
int height = dibsection.dsBm.bmHeight;
// zero out the RGB values for all pixels with A == 0
// (AlphaBlend expects them to all be zero)
for (int i = 0; i < dibsection.dsBmih.biWidth * dibsection.dsBmih.biHeight; i++)
{
IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));
if (rgbquad.rgbReserved == 0)
{
rgbquad.rgbBlue = 0;
rgbquad.rgbGreen = 0;
rgbquad.rgbRed = 0;
}
else
{
;
}
Marshal.StructureToPtr(rgbquad, ptr, false);
}
// create the destination Bitmap object
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// get the HDCs and select the HBITMAP
Graphics graphics = Graphics.FromImage(bitmap);
IntPtr hdcDest = graphics.GetHdc();
IntPtr hdcSrc = Gdi32.CreateCompatibleDC(hdcDest);
IntPtr hobjOriginal = Gdi32.SelectObject(hdcSrc, hbitmap);
// render the bitmap using AlphaBlend
BLENDFUNCTION blendfunction = new BLENDFUNCTION(BLENDFUNCTION.AC_SRC_OVER, 0, 0xFF, BLENDFUNCTION.AC_SRC_ALPHA);
Gdi32.AlphaBlend(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, width, height, blendfunction);
// clean up
Gdi32.SelectObject(hdcSrc, hobjOriginal);
Gdi32.DeleteDC(hdcSrc);
graphics.ReleaseHdc(hdcDest);
graphics.Dispose();
Gdi32.DeleteObject(hbitmap);
return bitmap;
}
}

It seems copying pixel by pixel was the solution. The following seems to be pixel-perfect equal to the explorer one.
public static Image GetIcon(string fileName, int size)
{
IShellItem shellItem;
Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);
IntPtr hbitmap;
((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);
// get the info about the HBITMAP inside the IPictureDisp
DIBSECTION dibsection = new DIBSECTION();
Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
int width = dibsection.dsBm.bmWidth;
int height = dibsection.dsBm.bmHeight;
// create the destination Bitmap object
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
for (int x = 0; x < dibsection.dsBmih.biWidth; x++)
{
for (int y = 0; y < dibsection.dsBmih.biHeight; y++)
{
int i = y * dibsection.dsBmih.biWidth + x;
IntPtr ptr = dibsection.dsBm.bmBits + (i * Marshal.SizeOf(typeof(RGBQUAD)));
var rgbquad = (RGBQUAD)Marshal.PtrToStructure(ptr, typeof(RGBQUAD));
if (rgbquad.rgbReserved != 0)
bitmap.SetPixel(x, y, Color.FromArgb(rgbquad.rgbReserved, rgbquad.rgbRed, rgbquad.rgbGreen, rgbquad.rgbBlue));
}
}
Gdi32.DeleteObject(hbitmap);
return bitmap;
}

Related

BitBlt screen capture not working on Windows 10

I'm using this code to capture a process window in the background:
IntPtr = Process.GetProcessByName("memu")[0].MainWindowHandle;
RECT rc;
GetClientRect(hwnd, out rc);
IntPtr hdcFrom = GetDC(hwnd);
IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
int Width = rc.right;
int Height = rc.bottom;
Bitmap bmp = null;
IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, Width, Height);
if (hBitmap != IntPtr.Zero) {
IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
BitBlt(hdcTo, 0, 0, Width, Height, hdcFrom, 0, 0, CopyPixelOperation.SourceCopy);
SelectObject(hdcTo, hLocalBitmap);
DeleteDC(hdcTo);
ReleaseDC(hwnd, hdcFrom);
bmp = Image.FromHbitmap(hBitmap);
DeleteObject(hBitmap);
return bmp;
}
This code is capture an Android emulator called MEmu, it is using DirectX to render the content. But this code stopped to work after Windows 10 updated to version 16299 (it was working normally before), it still working on Windows 7 with Aero mode enabled.
When I use this method in the Windows 10 Pro v16299.X it simply return a white image or it returns the emulator "loading screen", not the running content. On Windows 7, if I remove the Aero mode it will act the same, capturing the "loading screen", so looks like somehow the way the transparency works in the new windows 10 pro update changed.
I've tried everything, tried install some modules to force Aero Mode to work on Windows 10, tried PrintWindow to capture the screen in the background, but still the same.
Any ideas what could be happening? Or a possible solution? Or what changed in this last Windows 10 Pro version that could break that code?
Thank you!
For me this is working on Windows 10 11.02.2021:
static void hflipAndSwapRandB(unsigned char* data, int w, int h, int dim)
{
int y = 0;
int x = 0;
int d;
unsigned char swap = 0;
int hh = h / 2;
for (y = 0; y < hh; y++)
{
for (x = 0; x < w; x++)
{
for (d = 0; d < dim; d++)
{
swap = data[y * dim * w + dim * x + d];
data[y * dim * w + dim * x + d] = data[(h - 1 - y) * dim * w + dim * x + dim - 1 - d];
data[(h - 1 - y) * dim * w + dim * x + dim - 1 - d] = swap;
}
}
}
}
static void copyScreen(unsigned char* pixels_out, int x, int y, int width, int height)
{
HDC hScreenDC = GetDC(GetDesktopWindow());
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
if (width == -1 || height == -1)
{
width = GetDeviceCaps(hScreenDC, HORZRES);
height = GetDeviceCaps(hScreenDC, VERTRES);
}
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, x, y, width, height, hScreenDC, x, y, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width-x;
bi.biHeight = height-y;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
GetDIBits(hMemoryDC, hBitmap, 0, height-y, pixels_out, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
hflipAndSwapRandB(pixels_out, width, height, 3);
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
DeleteObject(hBitmap);
}
Hopefully this will solve the problem. There is a method for capturing the screen that is built in to the .net framework that may work. Not sure if it will capture DirectX content, but it may be worth a try.
Please note that this solution captures the current screen, but you will probably be able to modify it to capture only the area you are interested in.
I found this solution here: https://www.c-sharpcorner.com/UploadFile/2d2d83/how-to-capture-a-screen-using-C-Sharp/
private void CaptureMyScreen()
{
try
{
//Creating a new Bitmap object
Bitmap captureBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
//Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);
//Creating a Rectangle object which will
//capture our Current Screen
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
//Creating a New Graphics Object
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
//Copying Image from The Screen
captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);
//Saving the Image File (I am here Saving it in My E drive).
captureBitmap.Save(#"E:\Capture.jpg",ImageFormat.Jpeg);
//Displaying the Successfull Result
MessageBox.Show("Screen Captured");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I've had the same white window problem when capturing another application's window. I could capture the whole desktop though. One way around this was to capture the desktop portion occupied by the app, another solution was to elevate my program to run as administrator. Running as administrator allowed me to capture a specific app window (in my case a .Net 4.8 framework app the only change needed was requireAdministrator in app manifest requestedExecutionLevel).
public static System.Drawing.Image CaptureWindow(IntPtr handle, bool capturedesktop = false, bool capturemouse = true)
{
if (IsWindowVisible(handle))
{
IntPtr hdcSrc = GetWindowDC(handle);
RECT windowRect;
GetWindowRect(handle, out windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
if (capturedesktop)
hdcSrc = GetWindowDC(GetDesktopWindow());
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
if (capturedesktop)
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, windowRect.left, windowRect.top, GDI32.TernaryRasterOperations.SRCCOPY);
else
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.TernaryRasterOperations.SRCCOPY);
if (capturemouse)
{
try
{
CURSORINFO cursorInfo = new CURSORINFO();
cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
IntPtr hicon;
if (GetCursorInfo(ref cursorInfo))
{
if (cursorInfo.flags == CURSOR_SHOWING)
{
if ((hicon = CopyIcon(cursorInfo.hCursor)) != IntPtr.Zero)
{
ICONINFO iconInfo;
if (GetIconInfo(hicon, out iconInfo))
{
using (Bitmap maskBitmap = System.Drawing.Image.FromHbitmap(iconInfo.hbmMask))
{
DrawIconEx(hdcDest, cursorInfo.ptScreenPos.X - iconInfo.xHotspot - windowRect.left, cursorInfo.ptScreenPos.Y - iconInfo.yHotspot - windowRect.top, cursorInfo.hCursor, 0, 0, 0, IntPtr.Zero, 0x0003);
GDI32.DeleteObject(iconInfo.hbmMask);
DestroyIcon(hicon);
}
}
else
{
DestroyIcon(hicon);
}
}
}
}
} catch
{
}
}
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
ReleaseDC(handle, hdcSrc);
System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
} else
{
return null;
}
}

Saving Transparent PNG as Transparent GIF

I'm trying to Resizing a Transparent png and saving it as a single frame gif image.
Let skip resizing part, while you try to save a Transparent png as gif, you will see a black background in output gif:
Bitmap n = new Bitmap(targetPngPath);
n.Save(#"C:\1.gif", ImageFormat.Gif);
yes, I can make that black background into white, but it is not what I'm looking for. Even I can remove the black color using MakeTransparent Method, But it will remove about every black color in image and we will not have a standard transparent image.
we also can do a TRICK saving gif image, we keep extension in filename but we will save it as PNG Format like this:
n.Save(#"C:\1.gif", ImageFormat.Png);
But it is not also standard.
So is there any way to safely save a transparent png as a gif image with transparency?
PNG =
GIF =
GIF Saved With Photoshop =
This is because the built-in GIF encoder cannot handle the source well, unless it is already a 8 bpp image. You must convert your PNG image to a 256 color image first, then you can save it correctly with the GIF encoder.
public static void SaveGif(string fileName, Image image)
{
int bpp = Image.GetPixelFormatSize(image.PixelFormat);
if (bpp == 8)
{
image.Save(fileName, ImageFormat.Gif);
return;
}
// 1 and 4 bpp images are need to be converted, too; otherwise, gif encoder encodes the image from 32 bpp image resulting 256 color, no transparency
if (bpp < 8)
{
using (Image image8Bpp = ConvertPixelFormat(image, PixelFormat.Format8bppIndexed, null))
{
image8Bpp.Save(fileName, ImageFormat.Gif);
return;
}
}
// high/true color bitmap: obtaining the colors
// Converting always to 8 bpp pixel format; otherwise, gif encoder would convert it to 32 bpp first.
// With 8 bpp, gif encoder will preserve transparency and will save compact palette
// Note: This works well for 256 color images in a 32bpp bitmap. Otherwise, you might try to pass null as palette so a default palette will be used.
Color[] palette = GetColors((Bitmap)image, 256);
using (Image imageIndexed = ConvertPixelFormat(image, PixelFormat.Format8bppIndexed, palette))
{
imageIndexed.Save(fileName, ImageFormat.Gif);
}
}
// TODO: Use some quantizer
private static Color[] GetColors(Bitmap bitmap, int maxColors)
{
if (bitmap == null)
throw new ArgumentNullException("bitmap");
if (maxColors < 0)
throw new ArgumentOutOfRangeException("maxColors");
HashSet<int> colors = new HashSet<int>();
PixelFormat pixelFormat = bitmap.PixelFormat;
if (Image.GetPixelFormatSize(pixelFormat) <= 8)
return bitmap.Palette.Entries;
// 32 bpp source: the performant variant
if (pixelFormat == PixelFormat.Format32bppRgb ||
pixelFormat == PixelFormat.Format32bppArgb ||
pixelFormat == PixelFormat.Format32bppPArgb)
{
BitmapData data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadOnly, pixelFormat);
try
{
unsafe
{
byte* line = (byte*)data.Scan0;
for (int y = 0; y < data.Height; y++)
{
for (int x = 0; x < data.Width; x++)
{
int c = ((int*)line)[x];
// if alpha is 0, adding the transparent color
if ((c >> 24) == 0)
c = 0xFFFFFF;
if (colors.Contains(c))
continue;
colors.Add(c);
if (colors.Count == maxColors)
return colors.Select(Color.FromArgb).ToArray();
}
line += data.Stride;
}
}
}
finally
{
bitmap.UnlockBits(data);
}
}
else
{
// fallback: getpixel
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
int c = bitmap.GetPixel(x, y).ToArgb();
if (colors.Contains(c))
continue;
colors.Add(c);
if (colors.Count == maxColors)
return colors.Select(Color.FromArgb).ToArray();
}
}
}
return colors.Select(Color.FromArgb).ToArray();
}
private static Image ConvertPixelFormat(Image image, PixelFormat newPixelFormat, Color[] palette)
{
if (image == null)
throw new ArgumentNullException("image");
PixelFormat sourcePixelFormat = image.PixelFormat;
int bpp = Image.GetPixelFormatSize(newPixelFormat);
if (newPixelFormat == PixelFormat.Format16bppArgb1555 || newPixelFormat == PixelFormat.Format16bppGrayScale)
throw new NotSupportedException("This pixel format is not supported by GDI+");
Bitmap result;
// non-indexed target image (transparency preserved automatically)
if (bpp > 8)
{
result = new Bitmap(image.Width, image.Height, newPixelFormat);
using (Graphics g = Graphics.FromImage(result))
{
g.DrawImage(image, 0, 0, image.Width, image.Height);
}
return result;
}
int transparentIndex;
Bitmap bmp;
// indexed colors: using GDI+ natively
RGBQUAD[] targetPalette = new RGBQUAD[256];
int colorCount = InitPalette(targetPalette, bpp, (image is Bitmap) ? image.Palette : null, palette, out transparentIndex);
BITMAPINFO bmi = new BITMAPINFO();
bmi.icHeader.biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER));
bmi.icHeader.biWidth = image.Width;
bmi.icHeader.biHeight = image.Height;
bmi.icHeader.biPlanes = 1;
bmi.icHeader.biBitCount = (ushort)bpp;
bmi.icHeader.biCompression = BI_RGB;
bmi.icHeader.biSizeImage = (uint)(((image.Width + 7) & 0xFFFFFFF8) * image.Height / (8 / bpp));
bmi.icHeader.biXPelsPerMeter = 0;
bmi.icHeader.biYPelsPerMeter = 0;
bmi.icHeader.biClrUsed = (uint)colorCount;
bmi.icHeader.biClrImportant = (uint)colorCount;
bmi.icColors = targetPalette;
bmp = (image as Bitmap) ?? new Bitmap(image);
// Creating the indexed bitmap
IntPtr bits;
IntPtr hbmResult = CreateDIBSection(IntPtr.Zero, ref bmi, DIB_RGB_COLORS, out bits, IntPtr.Zero, 0);
// Obtaining screen DC
IntPtr dcScreen = GetDC(IntPtr.Zero);
// DC for the original hbitmap
IntPtr hbmSource = bmp.GetHbitmap();
IntPtr dcSource = CreateCompatibleDC(dcScreen);
SelectObject(dcSource, hbmSource);
// DC for the indexed hbitmap
IntPtr dcTarget = CreateCompatibleDC(dcScreen);
SelectObject(dcTarget, hbmResult);
// Copy content
BitBlt(dcTarget, 0, 0, image.Width, image.Height, dcSource, 0, 0, 0x00CC0020 /*TernaryRasterOperations.SRCCOPY*/);
// obtaining result
result = Image.FromHbitmap(hbmResult);
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// cleanup
DeleteDC(dcSource);
DeleteDC(dcTarget);
ReleaseDC(IntPtr.Zero, dcScreen);
DeleteObject(hbmSource);
DeleteObject(hbmResult);
ColorPalette resultPalette = result.Palette;
bool resetPalette = false;
// restoring transparency
if (transparentIndex >= 0)
{
// updating palette if transparent color is not actually transparent
if (resultPalette.Entries[transparentIndex].A != 0)
{
resultPalette.Entries[transparentIndex] = Color.Transparent;
resetPalette = true;
}
ToIndexedTransparentByArgb(result, bmp, transparentIndex);
}
if (resetPalette)
result.Palette = resultPalette;
if (!ReferenceEquals(bmp, image))
bmp.Dispose();
return result;
}
private static int InitPalette(RGBQUAD[] targetPalette, int bpp, ColorPalette originalPalette, Color[] desiredPalette, out int transparentIndex)
{
int maxColors = 1 << bpp;
// using desired palette
Color[] sourcePalette = desiredPalette;
// or, using original palette if it has fewer or the same amount of colors as requested
if (sourcePalette == null && originalPalette != null && originalPalette.Entries.Length > 0 && originalPalette.Entries.Length <= maxColors)
sourcePalette = originalPalette.Entries;
// or, using default system palette
if (sourcePalette == null)
{
using (Bitmap bmpReference = new Bitmap(1, 1, GetPixelFormat(bpp)))
{
sourcePalette = bmpReference.Palette.Entries;
}
}
// it is ignored if source has too few colors (rest of the entries will be black)
transparentIndex = -1;
bool hasBlack = false;
int colorCount = Math.Min(maxColors, sourcePalette.Length);
for (int i = 0; i < colorCount; i++)
{
targetPalette[i] = new RGBQUAD(sourcePalette[i]);
if (transparentIndex == -1 && sourcePalette[i].A == 0)
transparentIndex = i;
if (!hasBlack && (sourcePalette[i].ToArgb() & 0xFFFFFF) == 0)
hasBlack = true;
}
// if transparent index is 0, relocating it and setting transparent index to 1
if (transparentIndex == 0)
{
targetPalette[0] = targetPalette[1];
transparentIndex = 1;
}
// otherwise, setting the color of transparent index the same as the previous color, so it will not be used during the conversion
else if (transparentIndex != -1)
{
targetPalette[transparentIndex] = targetPalette[transparentIndex - 1];
}
// if black color is not found in palette, counting 1 extra colors because it can be used in conversion
if (colorCount < maxColors && !hasBlack)
colorCount++;
return colorCount;
}
private unsafe static void ToIndexedTransparentByArgb(Bitmap target, Bitmap source, int transparentIndex)
{
int sourceBpp = Image.GetPixelFormatSize(source.PixelFormat);
int targetBpp = Image.GetPixelFormatSize(target.PixelFormat);
BitmapData dataTarget = target.LockBits(new Rectangle(Point.Empty, target.Size), ImageLockMode.ReadWrite, target.PixelFormat);
BitmapData dataSource = source.LockBits(new Rectangle(Point.Empty, source.Size), ImageLockMode.ReadOnly, source.PixelFormat);
try
{
byte* lineSource = (byte*)dataSource.Scan0;
byte* lineTarget = (byte*)dataTarget.Scan0;
bool is32Bpp = sourceBpp == 32;
// scanning through the lines
for (int y = 0; y < dataSource.Height; y++)
{
// scanning through the pixels within the line
for (int x = 0; x < dataSource.Width; x++)
{
// testing if pixel is transparent (applies both argb and pargb)
if (is32Bpp && ((uint*)lineSource)[x] >> 24 == 0
|| !is32Bpp && ((ulong*)lineSource)[x] >> 48 == 0UL)
{
switch (targetBpp)
{
case 8:
lineTarget[x] = (byte)transparentIndex;
break;
case 4:
// First pixel is the high nibble
int pos = x >> 1;
byte nibbles = lineTarget[pos];
if ((x & 1) == 0)
{
nibbles &= 0x0F;
nibbles |= (byte)(transparentIndex << 4);
}
else
{
nibbles &= 0xF0;
nibbles |= (byte)transparentIndex;
}
lineTarget[pos] = nibbles;
break;
case 1:
// First pixel is MSB.
pos = x >> 3;
byte mask = (byte)(128 >> (x & 7));
if (transparentIndex == 0)
lineTarget[pos] &= (byte)~mask;
else
lineTarget[pos] |= mask;
break;
}
}
}
lineSource += dataSource.Stride;
lineTarget += dataTarget.Stride;
}
}
finally
{
target.UnlockBits(dataTarget);
source.UnlockBits(dataSource);
}
}
private static PixelFormat GetPixelFormat(int bpp)
{
switch (bpp)
{
case 1:
return PixelFormat.Format1bppIndexed;
case 4:
return PixelFormat.Format4bppIndexed;
case 8:
return PixelFormat.Format8bppIndexed;
case 16:
return PixelFormat.Format16bppRgb565;
case 24:
return PixelFormat.Format24bppRgb;
case 32:
return PixelFormat.Format32bppArgb;
case 48:
return PixelFormat.Format48bppRgb;
case 64:
return PixelFormat.Format64bppArgb;
default:
throw new ArgumentOutOfRangeException("bpp");
}
}
And the native types and methods:
private const int BI_RGB = 0;
private const int DIB_RGB_COLORS = 0;
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFO pbmi, int iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport("gdi32.dll")]
private static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
private static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[StructLayout(LayoutKind.Sequential)]
private struct RGBQUAD
{
internal byte rgbBlue;
internal byte rgbGreen;
internal byte rgbRed;
internal byte rgbReserved;
internal RGBQUAD(Color color)
{
rgbRed = color.R;
rgbGreen = color.G;
rgbBlue = color.B;
rgbReserved = 0;
}
}
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFO
{
public BITMAPINFOHEADER icHeader;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public RGBQUAD[] icColors;
}
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFOHEADER
{
internal uint biSize;
internal int biWidth;
internal int biHeight;
internal ushort biPlanes;
internal ushort biBitCount;
internal uint biCompression;
internal uint biSizeImage;
internal int biXPelsPerMeter;
internal int biYPelsPerMeter;
internal uint biClrUsed;
internal uint biClrImportant;
}
Update:
My Drawing Libraries are now free to download. It makes a SaveAsGif extension method available on the Image type:
using KGySoft.Drawing;
/// ...
using (var stream = new FileStream(targetPngPath, FileMode.Create))
{
// You can either use an arbitrary palette,
myPngBitmap.SaveAsGif(stream, myPngBitmap.GetColors(256));
// or, you can let the built-in encoder use dithering with a fixed palette.
// Pixel format is adjusted so transparency will be preserved.
myPngBitmap.SaveAsGif(stream, allowDithering: true);
}
This may help.
The Bitmap class does not save correctly with transparency.
You need to cast Bitmap to Image.
c# Bitmap.Save transparancy doesn't save in png
There are comments on the internet about .NET not saving Bitmaps with Transparency correctly .
Here is a good link for further reading, too much code to post.
http://forums.asp.net/t/1057792.aspx?ASP+NET+C+Making+an+Image+transparent

How Can I Combine Bitmap's RGBA in C#

I have a problem of Pixel.
I wanted to combine Bitmap's RGBA. And I got It.
But, haapend some problem.
First of all, show you my Code.
My Code:
private Bitmap getImg (IntPtr hWnd, int width, int height, int startX, int startY) {
IntPtr hDC = Win32.GetDC(hWnd);
IntPtr hMemDC = Win32.CreateCompatibleDC(hDC);
IntPtr hBitmap = Win32.CreateCompatibleBitmap(hDC, width, height);
if (hBitmap != IntPtr.Zero) {
IntPtr hOld = (IntPtr) Win32.SelectObject(hMemDC, hBitmap);
Win32.BitBlt(hMemDC, 0, 0, width, height, hDC, startX, startY, 13369376);
Win32.SelectObject(hMemDC, hOld);
Win32.DeleteDC(hMemDC);
Win32.ReleaseDC(hWnd, hDC);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
Win32.DeleteObject(hBitmap);
GC.Collect();
return bmp;
}
return null;
}
private void checkPixel (Bitmap img) {
try {
if (img != null) {
long value = 0;
for (int x = 0; x < img.Width; x++) {
for (int y = 0; y < img.Height; y++) {
Color pixel = img.GetPixel(x, y);
value += pixel.A + pixel.R + pixel.G + pixel.B;
}
}
Console.WriteLine("value : " + value);
}
} catch (Exception e) {
Console.WriteLine(e);
}
}
// And.. I used this way
Bitmap img = getImg(hWnd, width, height, startX, startY);
if (img != null) {
checkPixel(img);
}
So.. This code is working cool my computer.
But, work in another computer, "value" is different than my "value".
BMP is same. (Game Screen)
I don't understand this problem.
How can I solve this problem ?
Do you have a better idea ?

Bitmap.FromHbitmap erroring when called over RDP

Our application does some cursor manipulation to enable "relatively" nice drag drop animation on WinForms (at the time WPF wasn't an option). However when using the application over a RDP session it throws a generic GDI+ exception.
The method which throws this is this:
[DllImport("user32")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO pIconInfo);
[DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string lpFileName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyIcon(IntPtr hIcon);
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
public static Bitmap BitmapFromCursor(Cursor cur)
{
ICONINFO iInfo;
GetIconInfo(cur.Handle, out iInfo);
Bitmap bmp = Bitmap.FromHbitmap(iInfo.hbmColor);
DeleteObject(iInfo.hbmColor);
DeleteObject(iInfo.hbmMask);
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
Bitmap dstBitmap = new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0);
bmp.UnlockBits(bmData);
return new Bitmap(dstBitmap);
}
Specifically the line:
Bitmap bmp = Bitmap.FromHbitmap(iInfo.hbmColor);
When debugging hbmColor is 0, which means when running over RDP the call to GetIconInfo doesn't return the required information.
I can check for 0 and handle the special case, but is there anything I can do to make this work over RDP as it would do normally?
Edit
Here's the ICONINFO structure:
[StructLayout(LayoutKind.Sequential)]
struct ICONINFO
{
public bool fIcon; // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
// an icon; FALSE specifies a cursor.
public Int32 xHotspot; // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
// spot is always in the center of the icon, and this member is ignored.
public Int32 yHotspot; // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
// spot is always in the center of the icon, and this member is ignored.
public IntPtr hbmMask; // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
// this bitmask is formatted so that the upper half is the icon AND bitmask and the lower half is
// the icon XOR bitmask. Under this condition, the height should be an even multiple of two. If
// this structure defines a color icon, this mask only defines the AND bitmask of the icon.
public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
// structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND
// flag to the destination; subsequently, the color bitmap is applied (using XOR) to the
// destination by using the SRCINVERT flag.
}
From HABJAN's answer below I've added the comments from p/Invoke to the structure above. It looks like hbmMask contains the bitmap reference I'm after, but I'm afraid my bit manipulation skills are rather rusty. When p/Invoke says upper half / lower half - what is it inferring to?
Is it possible to get the black and white bitmap from this?
I think that this is due your RDP color depth. If your cursor is black and white only (via RDP), you will not get hbmColor value as this parameter is optional.
MSDN says:
hbmColor
Type: HBITMAP
Description: A handle to the icon color bitmap. This member can be optional if this structure defines a black and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination; subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag.
EDIT:
public static Bitmap BitmapFromCursor(Cursor cur)
{
ICONINFO iInfo;
GetIconInfo(cur.Handle, out iInfo);
Bitmap bmpColor = null;
if (iInfo.hbmColor != IntPtr.Zero) {
bmpColor = Bitmap.FromHbitmap(iInfo.hbmColor);
}
else {
bmpColor = new Bitmap(w,h);
// fill bmpColor with white colour
}
Bitmap bmpMask = Bitmap.FromHbitmap(iInfo.hbmMask);
DeleteObject(iInfo.hbmColor);
DeleteObject(iInfo.hbmMask);
// apply mask bitmap to color bitmap:
// http://stackoverflow.com/questions/3654220/alpha-masking-in-c-sharp-system-drawing
BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
Bitmap dstBitmap = new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0);
bmp.UnlockBits(bmData);
return new Bitmap(dstBitmap);
}
... i did not test this code, it's just to give you a brief info what to do...
With the help of HABJAN I was able to come up with a method to do the job. The reason I'm writing the answer here is because the bitmap mask you get from the handle contains two masks, so you have to select which version you want (as per the documentation).
public static Bitmap GetBitmapFromMask(IntPtr maskH)
{
using (var bothMasks = Bitmap.FromHbitmap(maskH))
{
int midY = bothMasks.Height / 2;
using (var mask = bothMasks.Clone(new Rectangle(0, midY, bothMasks.Width, midY), bothMasks.PixelFormat))
{
using (var input = new Bitmap(mask.Width, mask.Height))
{
using (var g = Graphics.FromImage(input))
{
using (var b = new SolidBrush(Color.FromArgb(255, 255, 255, 255)))
g.FillRectangle(b, 0, 0, input.Width, input.Height);
}
var output = new Bitmap(mask.Width, mask.Height, PixelFormat.Format32bppArgb);
var rect = new Rectangle(0, 0, input.Width, input.Height);
var bitsMask = mask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bitsInput = input.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bitsOutput = output.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
for (int y = 0; y < input.Height; y++)
{
byte* ptrMask = (byte*)bitsMask.Scan0 + y * bitsMask.Stride;
byte* ptrInput = (byte*)bitsInput.Scan0 + y * bitsInput.Stride;
byte* ptrOutput = (byte*)bitsOutput.Scan0 + y * bitsOutput.Stride;
for (int x = 0; x < input.Width; x++)
{
ptrOutput[4 * x] = ptrInput[4 * x]; // blue
ptrOutput[4 * x + 1] = ptrInput[4 * x + 1]; // green
ptrOutput[4 * x + 2] = ptrInput[4 * x + 2]; // red
ptrOutput[4 * x + 3] = ptrMask[4 * x]; // alpha
}
}
}
mask.UnlockBits(bitsMask);
input.UnlockBits(bitsInput);
output.UnlockBits(bitsOutput);
return output;
}
}
}
}
This is a basic copy of the answer linked by HABJAN - it doesn't seem to do either a logical AND or a logical XOR on the resulting bytes - none the less seems to do the required job.

Sometimes bitmaps are upside down when getting file thumbnails

I use this method to get thumbnails of files (keeping transparency...):
public static Image GetIcon(string fileName, int size)
{
IShellItem shellItem;
Shell32.SHCreateItemFromParsingName(fileName, IntPtr.Zero, Shell32.IShellItem_GUID, out shellItem);
IntPtr hbitmap;
((IShellItemImageFactory)shellItem).GetImage(new SIZE(size, size), 0x0, out hbitmap);
// get the info about the HBITMAP inside the IPictureDisp
DIBSECTION dibsection = new DIBSECTION();
Gdi32.GetObjectDIBSection(hbitmap, Marshal.SizeOf(dibsection), ref dibsection);
int width = dibsection.dsBm.bmWidth;
int height = dibsection.dsBm.bmHeight;
// create the destination Bitmap object
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
unsafe
{
// get a pointer to the raw bits
RGBQUAD* pBits = (RGBQUAD*)(void*)dibsection.dsBm.bmBits;
// copy each pixel manually
for (int x = 0; x < dibsection.dsBmih.biWidth; x++)
{
for (int y = 0; y < dibsection.dsBmih.biHeight; y++)
{
int offset = y * dibsection.dsBmih.biWidth + x;
if (pBits[offset].rgbReserved != 0)
{
bitmap.SetPixel(x, y, Color.FromArgb(pBits[offset].rgbReserved, pBits[offset].rgbRed, pBits[offset].rgbGreen, pBits[offset].rgbBlue));
}
}
}
}
Gdi32.DeleteObject(hbitmap);
return bitmap;
}
But sometimes the image is upside down. When getting the same image for 2nd, 3rd time it's not upside down. Is there any way to determine wether it is upside down or not? If there was any solution, the code below should work:
if (isUpsideDown)
{
int offset = (dibsection.dsBmih.biHeight - y - 1) * dibsection.dsBmih.biWidth + x;
}
else
{
int offset = y * dibsection.dsBmih.biWidth + x;
}
I came across the same problem. Images from the clipboard where upside down. I managed to find out, that you can check the Stride value to see if the image is reversed:
BitmapData d = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
bmp.UnlockBits(d);
if (d.Stride > 0)
{
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
If the Stride value is greater than zero, the image is reversed.
Andy

Categories