C# 如何在具有透明度和文本的图像上绘制矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15889637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I draw a rectangle onto an image with transparency and text
提问by griegs
This is my first graphics based project and to begin with I need to be able to draw a rectangle onto a bitmap with transparency and text.
这是我的第一个基于图形的项目,首先我需要能够在具有透明度和文本的位图上绘制一个矩形。
I'm not sure where to begin with this. I've done a little research but can't seem to find an article that will allow me to add a semi transparent rectangle to an image.
我不知道从哪里开始。我做了一些研究,但似乎找不到一篇文章可以让我向图像添加半透明矩形。
What I will have is an image stream that I need to be able to manipulate.
我将拥有的是一个我需要能够操作的图像流。
Can someone please point me in the right direction for this?
有人可以为此指出我正确的方向吗?
A site with source would be great as I have never done any GDI work before.
一个带有源代码的站点会很棒,因为我以前从未做过任何 GDI 工作。
采纳答案by Nicolas Brault
You can try something like this:
你可以尝试这样的事情:
// 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 );
Edit: the code to create a semi transparency gray brush
编辑:创建半透明灰色画笔的代码
Color customColor = Color.FromArgb(50, Color.Gray);
SolidBrush shadowBrush = new SolidBrush(customColor);
g.FillRectangles(shadowBrush, new RectangleF[]{rectFToFill});
回答by Paul Sasik
To get started you need to create a Graphics context from the image that you want to modify. See here.
首先,您需要从要修改的图像创建一个 Graphics 上下文。看这里。
// Create image.
Image imageFile = Image.FromFile("SampImag.bmp");
// Create graphics object for alteration.
Graphics newGraphics = Graphics.FromImage(imageFile);
Once you have the Graphics object you can use its many methods to draw onto the image. In your example you would use DrawRectanglemethod with an ARGB color to create a semi-transparent rectangle on your image.
一旦你有了 Graphics 对象,你就可以使用它的许多方法在图像上绘制。在您的示例中,您将使用带有 ARGB 颜色的DrawRectangle方法在图像上创建一个半透明矩形。
You can then display the image to a screen control or save it to disk.
然后,您可以将图像显示到屏幕控件或将其保存到磁盘。