紧凑框架:用实心刷填充矩形问题

时间:2010-01-11 04:17:14

标签: c# compact-framework

在我的移动项目中,我想填充半透明颜色的矩形。通过文档,我了解到我可以使用color.fromargb()创建实体画笔。但问题是我可以在color.fromargb(红色,绿色,蓝色)中传递三个参数而不是4个参数(alpha,红色,绿色,蓝色)。那么我该如何解决呢?我正在研究compact framework 2.0。提前谢谢。

2 个答案:

答案 0 :(得分:2)

紧凑框架支持表示32位ARGB值的overload taking just one Int32 parameter。从那篇文章:

  

32位ARGB值的字节顺序是AARRGGBB。

因此,如果您将R,G和B值知道为字节,则应该能够从中创建所需的值。类似的东西:

var argb = (Int32)(128 | r << 16 | g << 8 | b);
var color = Color.FromArgb(argb);

...根据下面@Andrew Cash的评论,如果你想调整透明度,你可以将alpha值左移24。

该代码未经测试,但您明白了。

答案 1 :(得分:1)

我认为您需要从Imaging API进行P / Invoke AlphaBlend。

/// <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定义为:

/// <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;
    }
}