[英]C# - Gradient over picture in picturebox
我已經在VB.NET中開發了一個應用程序,現在我將其移至C#。
到目前為止,一切進展順利,但我面臨一個問題。
我有一個在其中有圖片的pictureBox。 在此圖片框上,我希望漸變從頂部透明到顏色“控件”,以與表單背景色融合。 我已經在可以正常工作的VB.net中完成了此操作,但是當我嘗試在C#中執行此操作時,似乎繪制了漸變,但是在圖片后面。
這是我嘗試過的:
private void PictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Color top = Color.Transparent;
Color bottom = Color.FromKnownColor(KnownColor.Control);
GradientPictureBox(top, bottom, ref PictureBox1, e);
}
public void GradientPictureBox(Color topColor, Color bottomColor, ref PictureBox PictureBox1, System.Windows.Forms.PaintEventArgs e)
{
LinearGradientMode direction = LinearGradientMode.Vertical;
LinearGradientBrush brush = new LinearGradientBrush(PictureBox1.DisplayRectangle, topColor, bottomColor, direction);
e.Graphics.FillRectangle(brush, PictureBox1.DisplayRectangle);
brush.Dispose();
}
但是,這實際上似乎確實有效,但是它再次在圖片后面繪制了漸變。 在VB.net中,它無需任何額外代碼即可將其繪制在圖片上方。
我是否需要添加任何其他內容?
如果有關系,請在C#2010 Express中進行即時編碼。
以下代碼可以做到這一點。
我可能會考慮使其成為自己的控件,並使用以下代碼作為其Paint事件。
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox1.Image, 0, 0, pictureBox1.ClientRectangle, GraphicsUnit.Pixel);
Color top = Color.FromArgb(128, Color.Blue);
Color bottom = Color.FromArgb(128, Color.Red);
LinearGradientMode direction = LinearGradientMode.Vertical;
LinearGradientBrush brush = new LinearGradientBrush(pictureBox1.ClientRectangle, top, bottom, direction);
e.Graphics.FillRectangle(brush, pictureBox1.ClientRectangle);
}
此代碼產生以下圖像
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.