AlphaBlend in CF

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);

}
}
}
 
G

Guest

Define "not working." Do you get an exception? What exception? Where? I
don't see any checks for error codes - are you checking? If so, what errors
do you see?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


Ram Prasath said:
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);

}
}
}
 
G

Guest

Hi,

It not returns any exception in application.The result of the AlphaBlend api
was Zero and also the alphablend effect was not reflected in the image.I used
the source image of Bit Depth 24 bpb.Is there be any issue on that.I run the
application in Win CE 6.0.

Define "not working." Do you get an exception? What exception? Where? I
don't see any checks for error codes - are you checking? If so, what errors
do you see?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


Ram Prasath said:
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);

}
}
}
 
J

jonfroehlich

Admittedly, I did not take a look at your code but I have implemented
AlphaBlending in one of my projects called Roam. You can view the code
here: http://sourceforge.net/projects/roam (download it via
SubVersion).

Here are two videos highlighting alpha blending on WM5 using .NET CF
2:
1.
2.

Run the sample application Roam.Test to play around with alpha-
blending.

Hope this helps,

Jon

----
http://csharponphone.blogspot.com

Hi,

It not returns any exception in application.The result of the AlphaBlend api
was Zero and also the alphablend effect was not reflected in the image.I used
the source image of Bit Depth 24 bpb.Is there be any issue on that.I run the
application in Win CE 6.0.

Define "not working." Do you get an exception? What exception? Where? I
don't see any checks for error codes - are you checking? If so, what errors
do you see?

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
Ram Prasath said:
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);
}
}
}
 
G

Guest

Hi,

Unfortunately i am not able to download the sample application.I am using
win ce 6.0.Did u provide a sample code of AlphaBlending in
netCompactFramework.

Thanks
Ram Prasath

jonfroehlich said:
Admittedly, I did not take a look at your code but I have implemented
AlphaBlending in one of my projects called Roam. You can view the code
here: http://sourceforge.net/projects/roam (download it via
SubVersion).

Here are two videos highlighting alpha blending on WM5 using .NET CF
2:
1.
2.

Run the sample application Roam.Test to play around with alpha-
blending.

Hope this helps,

Jon

----
http://csharponphone.blogspot.com

Hi,

It not returns any exception in application.The result of the AlphaBlend api
was Zero and also the alphablend effect was not reflected in the image.I used
the source image of Bit Depth 24 bpb.Is there be any issue on that.I run the
application in Win CE 6.0.

Define "not working." Do you get an exception? What exception? Where? I
don't see any checks for error codes - are you checking? If so, what errors
do you see?

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top