G
Guest
Hai,
I am using AlphaBlend Api in .NetCF using PINVOKE.AlphaBlending is not
working.
I will Provide the C# code below.
namespace Transparency
{
public partial class Form1 : Form
{
Graphics gxBuffer;
Bitmap backImage;
Bitmap offBitmap;
Bitmap topBar;
Bitmap slideAni;
Font timeFont;
Font dateFont;
string time = "";
string date = "";
string path = "";
Brush whiteBrush;
[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);
public Form1()
{
InitializeComponent();
string path =
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
slideAni = new Bitmap(path + @"\slideani.bmp");
backImage = new Bitmap(path + @"\wallpaper.bmp");
topBar = new Bitmap(path + @"\topbar.bmp");
timeFont = new Font("Tahoma", 32, FontStyle.Regular);
dateFont = new Font("Tahoma", 9, FontStyle.Regular);
whiteBrush = new SolidBrush(Color.White);
time = DateTime.Now.ToString("hh:mm");
date = DateTime.Now.DayOfWeek.ToString() + ", " +
DateTime.Now.ToString("MMMM") + " " + DateTime.Now.Day.ToString();
offBitmap = new Bitmap(this.Width, this.Height);
gxBuffer = Graphics.FromImage(offBitmap);
}
public struct BlendFunction
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
public enum BlendOperation : byte
{
AC_SRC_OVER = 0x00
}
public enum BlendFlags : byte
{
Zero = 0x00
}
public enum SourceConstantAlpha : byte
{
Transparent = 0x00,
Opaque = 0xFF
}
public enum AlphaFormat : byte
{
AC_SRC_ALPHA = 0xFF
}
protected override void OnPaint(PaintEventArgs e)
{
gxBuffer.Clear(this.BackColor);
gxBuffer.DrawImage(backImage, 0, 0);
DrawAlpha(gxBuffer, topBar, 200, 0, 60);
DrawTime(time, gxBuffer, 72);
SizeF size = gxBuffer.MeasureString(date, dateFont);
int x = this.Width / 2 - (int)size.Width / 2;
gxBuffer.DrawString(date, dateFont, whiteBrush, x, 70);
DrawAlpha(gxBuffer, slideAni, 70, 0, 258);
e.Graphics.DrawImage(offBitmap, 0, 0);
//this.Invalidate(new Rectangle(0, 60, 240, 70));
//this.Invalidate();
}
private void DrawAlpha(Graphics gx, Bitmap image, byte transp, int
x, int y)
{
using (Graphics gxSrc = Graphics.FromImage(image))
{
int result;
IntPtr hdcDst = gx.GetHdc();
IntPtr hdcSrc = gxSrc.GetHdc();
BlendFunction blendFunction = new BlendFunction();
blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;
// Only supported blend operation
blendFunction.BlendFlags = (byte)BlendFlags.Zero;
// Documentation says put 0 here
blendFunction.SourceConstantAlpha = transp;// Constant
alpha factor
blendFunction.AlphaFormat = (byte)0;
// Don't look for per pixel alpha
result = AlphaBlend(hdcDst, x, y, image.Width, image.Height,
hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
if (result == 0)
{
int lastError = Marshal.GetLastWin32Error();
//throw new Win32Exception(Marshal.GetLastWin32Error());
}
gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc()
gxSrc.ReleaseHdc(hdcSrc); // Required cleanup to
GetHdc()
}
}
private void DrawTime(string time, Graphics gx, int y)
{
SizeF size = gx.MeasureString(time, timeFont);
int x = this.Width / 2 - (int)size.Width / 2;
gx.DrawString(time, timeFont, new SolidBrush(Color.White), x, y);
}
}
}
I am using AlphaBlend Api in .NetCF using PINVOKE.AlphaBlending is not
working.
I will Provide the C# code below.
namespace Transparency
{
public partial class Form1 : Form
{
Graphics gxBuffer;
Bitmap backImage;
Bitmap offBitmap;
Bitmap topBar;
Bitmap slideAni;
Font timeFont;
Font dateFont;
string time = "";
string date = "";
string path = "";
Brush whiteBrush;
[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);
public Form1()
{
InitializeComponent();
string path =
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
slideAni = new Bitmap(path + @"\slideani.bmp");
backImage = new Bitmap(path + @"\wallpaper.bmp");
topBar = new Bitmap(path + @"\topbar.bmp");
timeFont = new Font("Tahoma", 32, FontStyle.Regular);
dateFont = new Font("Tahoma", 9, FontStyle.Regular);
whiteBrush = new SolidBrush(Color.White);
time = DateTime.Now.ToString("hh:mm");
date = DateTime.Now.DayOfWeek.ToString() + ", " +
DateTime.Now.ToString("MMMM") + " " + DateTime.Now.Day.ToString();
offBitmap = new Bitmap(this.Width, this.Height);
gxBuffer = Graphics.FromImage(offBitmap);
}
public struct BlendFunction
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
public enum BlendOperation : byte
{
AC_SRC_OVER = 0x00
}
public enum BlendFlags : byte
{
Zero = 0x00
}
public enum SourceConstantAlpha : byte
{
Transparent = 0x00,
Opaque = 0xFF
}
public enum AlphaFormat : byte
{
AC_SRC_ALPHA = 0xFF
}
protected override void OnPaint(PaintEventArgs e)
{
gxBuffer.Clear(this.BackColor);
gxBuffer.DrawImage(backImage, 0, 0);
DrawAlpha(gxBuffer, topBar, 200, 0, 60);
DrawTime(time, gxBuffer, 72);
SizeF size = gxBuffer.MeasureString(date, dateFont);
int x = this.Width / 2 - (int)size.Width / 2;
gxBuffer.DrawString(date, dateFont, whiteBrush, x, 70);
DrawAlpha(gxBuffer, slideAni, 70, 0, 258);
e.Graphics.DrawImage(offBitmap, 0, 0);
//this.Invalidate(new Rectangle(0, 60, 240, 70));
//this.Invalidate();
}
private void DrawAlpha(Graphics gx, Bitmap image, byte transp, int
x, int y)
{
using (Graphics gxSrc = Graphics.FromImage(image))
{
int result;
IntPtr hdcDst = gx.GetHdc();
IntPtr hdcSrc = gxSrc.GetHdc();
BlendFunction blendFunction = new BlendFunction();
blendFunction.BlendOp = (byte)BlendOperation.AC_SRC_OVER;
// Only supported blend operation
blendFunction.BlendFlags = (byte)BlendFlags.Zero;
// Documentation says put 0 here
blendFunction.SourceConstantAlpha = transp;// Constant
alpha factor
blendFunction.AlphaFormat = (byte)0;
// Don't look for per pixel alpha
result = AlphaBlend(hdcDst, x, y, image.Width, image.Height,
hdcSrc, 0, 0, image.Width, image.Height, blendFunction);
if (result == 0)
{
int lastError = Marshal.GetLastWin32Error();
//throw new Win32Exception(Marshal.GetLastWin32Error());
}
gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc()
gxSrc.ReleaseHdc(hdcSrc); // Required cleanup to
GetHdc()
}
}
private void DrawTime(string time, Graphics gx, int y)
{
SizeF size = gx.MeasureString(time, timeFont);
int x = this.Width / 2 - (int)size.Width / 2;
gx.DrawString(time, timeFont, new SolidBrush(Color.White), x, y);
}
}
}