In my mobile project, I want to fill a rectangle with semi transparent color. Through documents, I learn that I can create solid brush with color.fromargb(). But the problem is that I can just pass three parameter in the color.fromargb(Red, Green, Blue) instead of 4 params (alpha, red, green, blue). So how can I fix it? I'm working on compact framework 2.0. Thanks in advance.
The compact framework supports an overload taking just one Int32 parameter representing a 32-bit ARGB value. From that article:
The byte-ordering of the 32-bit ARGB value is AARRGGBB.
So if you know your R, G and B values as bytes, you should be able to create the value you want from that. Something like:
var argb = (Int32)(128 | r << 24 | g << 16 | b);
var color = Color.FromArgb(argb);
That code's untested, but you get the idea.
Thanks for your quick reply. I applied your document, but maybe something's wrong happen. For example, I create a solid brush
SolidBrush StringBrush = new SolidBrush(Color.FromArgb(0x78000000));
then when I draw my own Image, I want to fill a semi transparent rectangle onto that Image.
//Draw Image
myGraph.DrawImage(myCurrentImg, new Rectangle(0, 0, UnitWidth, UnitHeight), new Rectangle(0, 0, currentImg.Width, currentImg.Height), GraphicsUnit.Pixel);
// Fill a semi - transparent rectangle
myGraph.FillRectangle(StringBrush, ImgLocX, ImgLocY, UnitWidth, UnitHeight);
//Then write something onto the Image.
myGraph.DrawString(myStr, font, new SolidBrush(Color.Black), ImgLocX + StringHorMargin, ImgLocY + StringVerMargin);
But not work. Where was I wrong?
P.S: Code is too long to add as a comment.
I think you are going to need to P/Invoke AlphaBlend from the Imaging API.
/// <summary>
/// The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.
/// </summary>
/// <param name="hdcDest">[in] Handle to the destination device context.</param>
/// <param name="xDest">[in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param>
/// <param name="yDest">[in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param>
/// <param name="cxDest">[in] Specifies the width, in logical units, of the destination rectangle.</param>
/// <param name="cyDest">[in] Specifies the height, in logical units, of the destination rectangle.</param>
/// <param name="hdcSrc">[in] Handle to the source device context.</param>
/// <param name="xSrc">[in] Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.</param>
/// <param name="ySrc">[in] Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.</param>
/// <param name="cxSrc">[in] Specifies the width, in logical units, of the source rectangle.</param>
/// <param name="cySrc">[in] Specifies the height, in logical units, of the source rectangle.</param>
/// <param name="blendFunction">[in] Specifies the alpha-blending function for source and destination bitmaps, a global alpha value to be applied to the entire source bitmap, and format information for the source bitmap. The source and destination blend functions are currently limited to AC_SRC_OVER. See the BLENDFUNCTION and EMRALPHABLEND structures.</param>
/// <returns>If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. </returns>
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AlphaBlend(
IntPtr hdcDest,
int xDest,
int yDest,
int cxDest,
int cyDest,
IntPtr hdcSrc,
int xSrc,
int ySrc,
int cxSrc,
int cySrc,
BlendFunction blendFunction);
BlendFunction
is defined as:
/// <summary>
/// This structure controls blending by specifying the blending functions for source and destination bitmaps.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct BlendFunction
{
/// <summary>
/// Specifies the source blend operation. Currently, the only source and destination blend operation that has been defined is AC_SRC_OVER. For details, see the following Remarks section.
/// </summary>
public byte BlendOp;
/// <summary>
/// Must be zero.
/// </summary>
public byte BlendFlags;
/// <summary>
/// Specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values in the source bitmap. If you set SourceConstantAlpha to 0, it is assumed that your image is transparent. When you only want to use per-pixel alpha values, set the SourceConstantAlpha value to 255 (opaque) .
/// </summary>
public byte SourceConstantAlpha;
/// <summary>
/// This member controls the way the source and destination bitmaps are interpreted. The following table shows the value for AlphaFormat.
/// ---
/// AC_SRC_ALPHA This flag is set when the bitmap has an Alpha channel (that is, per-pixel alpha). Because this API uses premultiplied alpha, the red, green and blue channel values in the bitmap must be premultiplied with the alpha channel value. For example, if the alpha channel value is x, the red, green and blue channels must be multiplied by x and divided by 0xff before the call.
/// </summary>
public byte AlphaFormat;
/// <summary>
/// Initializes a new instance of the <see cref="BlendFunction"/> structure.
/// </summary>
/// <param name="alphaConst">Specifies an alpha transparency value to be used on the entire source bitmap.
/// </param>
/// <param name="alphaFormat">Alpha flag
/// </param>
public BlendFunction(byte alphaConst, byte alphaFormat)
{
this.BlendOp = 0;
this.BlendFlags = 0;
this.SourceConstantAlpha = alphaConst;
this.AlphaFormat = alphaFormat;
}
}