2013-04-08 80 views
10

這是我的第一個基於圖形的項目,首先我需要能夠在具有透明度和文本的位圖上繪製矩形。如何在帶有透明度和文本的圖像上繪製矩形

我不確定從哪裏開始。我已經做了一些研究,但似乎找不到能夠讓我爲圖像添加半透明矩形的文章。

我將有一個圖像流,我需要能夠操縱。

有人可以請指出我在這個正確的方向嗎?

與源代碼的網站將是偉大的,因爲我從來沒有做過任何GDI工作。

回答

25

你可以嘗試這樣的事情:

// 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}); 
+0

+1尼斯和簡潔 – 2013-04-08 22:04:44

+0

真棒。謝謝。 – griegs 2013-04-08 22:30:01

4

要開始,你需要創建一個從圖像的圖形上下文你想修改。 See here.

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

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

一旦你有了圖形對象,你可以使用它的許多方法來繪製圖像。在您的示例中,您將使用DrawRectangle方法和ARGB顏色在圖像上創建一個半透明的矩形。

然後,可以顯示圖像的屏幕控制或將其保存到磁盤。

+0

+1,對,我想我明白了。謝謝。 – griegs 2013-04-08 22:09:39