【问题标题】:How do I draw a rectangle onto an image with transparency and text如何在具有透明度和文本的图像上绘制矩形
【发布时间】:2013-03-31 03:45:57
【问题描述】:

这是我的第一个基于图形的项目,首先我需要能够在具有透明度和文本的位图上绘制一个矩形。

我不知道从哪里开始。我做了一些研究,但似乎找不到可以让我在图像中添加半透明矩形的文章。

我将拥有一个我需要能够操作的图像流。

有人可以为我指出正确的方向吗?

有源代码的网站会很棒,因为我以前从未做过任何 GDI 工作。

【问题讨论】:

    标签: c# gdi+


    【解决方案1】:

    你可以试试这样的:

    // Load the image (probably from your stream)
    Image image = Image.FromFile( imagePath );
    
    using (Graphics g = Graphics.FromImage(image))
    {
       // Modify the image using g here... 
       // Create a brush with an alpha value and use the g.FillRectangle function
    }
    
    image.Save( imageNewPath );
    

    编辑:创建半透明灰色画笔的代码

    Color customColor = Color.FromArgb(50, Color.Gray);
    SolidBrush shadowBrush = new SolidBrush(customColor);
    g.FillRectangles(shadowBrush, new RectangleF[]{rectFToFill});
    

    【讨论】:

      【解决方案2】:

      首先,您需要从要修改的图像创建一个图形上下文。 See here.

      // Create image.
      Image imageFile = Image.FromFile("SampImag.bmp");
      
      // Create graphics object for alteration.
      Graphics newGraphics = Graphics.FromImage(imageFile);
      

      一旦有了 Graphics 对象,您就可以使用它的许多方法在图像上绘图。在您的示例中,您将使用带有 ARGB 颜色的 DrawRectangle 方法在图像上创建一个半透明的矩形。

      然后您可以将图像显示到屏幕控件或将其保存到磁盘。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-16
        • 2020-10-24
        • 2016-05-25
        • 2016-10-05
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 2011-01-27
        相关资源
        最近更新 更多