您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#如何使用Effects給圖片增加陰影效果”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C#如何使用Effects給圖片增加陰影效果”吧!
代碼如下:
using (var imageStreamSource = File.OpenRead(@"r:\4.png")) using (Stream fs = File.Create(@"r:\test.png")) { var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); var bitmapFrame = decoder.Frames[0]; var size = new Size(bitmapFrame.PixelWidth, bitmapFrame.PixelHeight); var img = new Image() { Source = bitmapFrame }; img.Effect = new System.Windows.Media.Effects.DropShadowEffect(); img.Arrange(new Rect(0,0,bitmapFrame.PixelWidth,bitmapFrame.PixelHeight)); var rtb = new RenderTargetBitmap(bitmapFrame.PixelWidth, bitmapFrame.PixelHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(img); var png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(rtb)); png.Save(fs); }
使用過(guò)程中,發(fā)現(xiàn)WPF和GDI的處理方式還是有有些類似的。它的基本使用方式如下:
Image myImage = new Image(); FormattedText text = new FormattedText("ABC", new CultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()), this.FontSize, this.Foreground); DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawText(text, new Point(2, 2)); drawingContext.Close(); RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32); bmp.Render(drawingVisual); myImage.Source = bmp;
主要是如下幾步:
在DrawingContext中繪圖
通過(guò)DrawingVisual將DrawingContext轉(zhuǎn)換為Visual
通過(guò)RenderTargetBitmap將Visual轉(zhuǎn)換為BitmapFrame
通過(guò)xxxBitmapEncoder將BitmapFrame保存為圖像
這些步驟也無(wú)需嚴(yán)格遵守,像我最開(kāi)始的那個(gè)例子則是直接生成Visual,然后保存為圖像。其實(shí)我更喜歡這種方式,因?yàn)閂isual是可以直接在WPF的界面上顯示出來(lái)的,方便調(diào)試,并且很方便應(yīng)用WPF中的各種特效。不過(guò)要記得調(diào)用一下Arrange函數(shù),否則看不到生成結(jié)果的。
這里再給個(gè)更簡(jiǎn)單的例子,以供學(xué)習(xí)。
Ellipse cir = new Ellipse(); cir.Height = 50; cir.Width = 50; cir.Stroke = Brushes.Black; cir.StrokeThickness = 1.0; cir.Arrange(new Rect(new Size(50, 50))); //這句不能漏了 RenderTargetBitmap rtb = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32); rtb.Render(cir); PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(rtb)); using (Stream fs = File.Create(@"r:\test.png")) { png.Save(fs); }
感謝各位的閱讀,以上就是“C#如何使用Effects給圖片增加陰影效果”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C#如何使用Effects給圖片增加陰影效果這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。