2010-01-11 74 views
2

在我的移動項目中,我想用半透明顏色填充矩形。通過文檔,我瞭解到我可以使用color.fromargb()創建純色筆刷。但問題是我只能通過color.fromargb(紅色,綠色,藍色)三個參數而不是四個參數(alpha,紅色,綠色,藍色)。那我該如何解決它?我正在開發緊湊框架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現金的評論,你可以離開了,如果你想調整透明度24轉移阿爾法值。

該代碼未經測試,但您明白了。

+0

變種顏色= Color.FromArgb((的Int32)(A << 24 | R << 16 | G << 8 | B)); – 2012-04-08 08:15:25

+0

@Andrew Blast從過去看這個答案!雖然發現了 - 我會解決它。 – 2012-04-08 08:19:46

1

我認爲您需要從Imaging API中調用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; 
    } 
} 
+0

http://blogs.msdn.com/chrislorton/archive/2006/04/07/570649.aspx – Bryan 2010-01-27 22:17:22