Reputation: 1270
I'm designing a Compact Framework Control that supports transparency and for the runtime version of the control everything works just find doing the platform invoke on this API:
[DllImport("coredll.dll")]
extern public static Int32 AlphaBlend(IntPtr hdcDest, Int32 xDest, Int32 yDest, Int32 cxDest, Int32 cyDest, IntPtr hdcSrc, Int32 xSrc, Int32 ySrc, Int32 cxSrc, Int32 cySrc, BlendFunction blendFunction);
Obviously a call to "coredll.dll" isn't going to work in the desktop design time experience and for now, when the painting happens, I'm simply detecting that the control is being designed and painting it without any transparency. I would like to be able to give a better design time experience and show that same transparency in the Visual Studio Designer.
I've tried making this platform call:
[DllImport("gdi32.dll", EntryPoint = "GdiAlphaBlend")]
public static extern bool AlphaBlendDesktop(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
int nWidthDest, int nHeightDest,
IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
BlendFunction blendFunction);
but while it returns true, the result is nothing at all is painted to the design time view.
Any thoughts?
Upvotes: 1
Views: 2749
Reputation:
This worked for me to fool the designer to perform AlphaBlend operations.
// PixelFormatIndexed = 0x00010000, // Indexes into a palette
// PixelFormatGDI = 0x00020000, // Is a GDI-supported format
// PixelFormatAlpha = 0x00040000, // Has an alpha component
// PixelFormatPAlpha = 0x00080000, // Pre-multiplied alpha
// PixelFormatExtended = 0x00100000, // Extended color 16 bits/channel
// PixelFormatCanonical = 0x00200000,
// PixelFormat32bppARGB = (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),
// cheat the design time to create a bitmap with an alpha channel
using (Bitmap bmp = new Bitmap(image.Width, image.Height, (PixelFormat)
PixelFormat32bppARGB))
{
// copy the original image
using(Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(image,0,0);
}
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
(PixelFormat)PixelFormat32bppARGB);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 32 bits per pixels.
int bytes = bmp.Width * bmp.Height * 4;
byte[] argbValues = new byte[bytes];
// Copy the ARGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, bytes);
// Set every alpha value to the given transparency.
for (int counter = 3; counter < argbValues.Length; counter += 4)
argbValues[counter] = transparency;
// Copy the ARGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
gx.DrawImage(bmp, x, y);
}
Upvotes: 2
Reputation: 11736
Sorry that this is not exactly answering your question...but you should check out the UI Framework for .Net Compact Framework, which does alpha blending already.
http://code.msdn.microsoft.com/uiframework
Upvotes: 0