2016-10-21 7 views
0

線形グラデーションの不透明度を画像に適用したいが、私が得ることができたのは画像の上に塗りつぶしたグラデーションだけだった。LinearGradientBrushの不透明度をwinformsの画像に適用する

enter image description here

this stackoverflow後に続いて、私はピクチャから継承するユーザーコントロールを作成し、私は単に画像の上に直線勾配を塗料のOnPaintメソッドが

protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
      this.ClientRectangle, 
      Color.FromArgb(255, Color.White), 
      Color.FromArgb(0, Color.White), 
      0f); 
     e.Graphics.FillRectangle(linearGradientBrush, this.ClientRectangle); 
    } 

をオーバーライド。

リニアグラデーションの不透明度をに適用するにはどうすればよいですか?

私はXAML()で例を見つけるが、winformsでは見つからない。

答えて

3

ユーザーコントロールを使用して、OnPaintイベントを上書きする必要はありません。空白の画像からグラフィックスオブジェクトを作成し、画像操作を行い、画像をPicturePoxに割り当てます。

最初にlinearGradientBrushを背景に描画し、その後にその上に画像を描画します。常にイメージ操作の順序に注意してください。フォームの背景色と透明度を有する例示画像としてブラックと

FileInfo inputImageFile = new FileInfo(@"C:\Temp\TheSimpsons.png"); 
Bitmap inputImage = (Bitmap)Bitmap.FromFile(inputImageFile.FullName); 

// create blank bitmap with same size 
Bitmap combinedImage = new Bitmap(inputImage.Width, inputImage.Height); 

// create graphics object on new blank bitmap 
Graphics g = Graphics.FromImage(combinedImage); 

// also use the same size for the gradient brush and for the FillRectangle function 
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
    new Rectangle(0,0,combinedImage.Width, combinedImage.Height), 
    Color.FromArgb(255, Color.White), //Color.White, 
    Color.FromArgb(0, Color.White), // Color.Transparent, 
    0f); 
g.FillRectangle(linearGradientBrush, 0, 0, combinedImage.Width, combinedImage.Height); 

g.DrawImage(inputImage, 0,0); 

previewPictureBox.Image = combinedImage; 

result

結果。

EDIT: 私は意図を誤解している可能性があり、WPFのような簡単な方法を見つけられなかったかもしれませんが、それほど難しいことではありません。

FileInfo inputImageFile = new FileInfo(@"C:\Temp\TheSimpsons.png"); 
Bitmap inputImage = (Bitmap)Bitmap.FromFile(inputImageFile.FullName); 

// create blank bitmap 
Bitmap adjImage = new Bitmap(inputImage.Width, inputImage.Height); 

// create graphics object on new blank bitmap 
Graphics g = Graphics.FromImage(adjImage); 

LinearGradientBrush linearGradientBrush = new LinearGradientBrush(
    new Rectangle(0, 0, adjImage.Width, adjImage.Height), 
    Color.White, 
    Color.Transparent, 
    0f); 

Rectangle rect = new Rectangle(0, 0, adjImage.Width, adjImage.Height); 
g.FillRectangle(linearGradientBrush, rect); 

int x; 
int y; 
for (x = 0; x < adjImage.Width; ++x) 
{ 
    for (y = 0; y < adjImage.Height; ++y) 
    { 
     Color inputPixelColor = inputImage.GetPixel(x, y); 
     Color adjPixelColor = adjImage.GetPixel(x, y); 
     Color newColor = Color.FromArgb(adjPixelColor.A, inputPixelColor.R, inputPixelColor.G, inputPixelColor.B); 
     adjImage.SetPixel(x, y, newColor); 
    } 
} 
previewPictureBox.Image = adjImage; 

enter image description here

+0

うわー!私は最初に尋ねたとき誰もこの質問に答えなかったので、誰もそれを望んでいないと思った。私はあなたがこの質問をその後ずっと見つけて、それに答えてくれたことにうれしく驚いています。ありがとう!だから、私はあなたが言っていることを見ています。 OK、それを試してみます。ありがとう、 –

関連する問題