23

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

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

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

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

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

4

2 回答 2

38

你可以尝试这样的事情:

// 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});
于 2013-04-08T22:01:57.960 回答
5

要开始使用,您需要从要修改的图像创建一个 Graphics 上下文。看这里。

// Create image.
Image imageFile = Image.FromFile("SampImag.bmp");

// Create graphics object for alteration.
Graphics newGraphics = Graphics.FromImage(imageFile);

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

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

于 2013-04-08T22:04:10.933 回答