这篇文章介绍了C#使用Effects给图片增加阴影效果的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
今天写一个小程序有一个给图片加上阴影的需求,记得WPF的Effect中就有阴影特效,就打算用它了。代码如下:
    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);
    }使用过程中,发现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中绘图
 - 通过DrawingVisual将DrawingContext转换为Visual
 - 通过RenderTargetBitmap将Visual转换为BitmapFrame
 - 通过xxxBitmapEncoder将BitmapFrame保存为图像
 
这些步骤也无需严格遵守,像我最开始的那个例子则是直接生成Visual,然后保存为图像。其实我更喜欢这种方式,因为Visual是可以直接在WPF的界面上显示出来的,方便调试,并且很方便应用WPF中的各种特效。不过要记得调用一下Arrange函数,否则看不到生成结果的。
这里再给个更简单的例子,以供学习。
    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给图片增加阴影效果的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
				 沃梦达教程
				
			本文标题为:C#使用Effects给图片增加阴影效果
				
        
 
            
        
             猜你喜欢
        
	     - 在C# 8中如何使用默认接口方法详解 2023-03-29
 - WPF使用DrawingContext实现绘制刻度条 2023-07-04
 - user32.dll 函数说明小结 2022-12-26
 - Unity3D实现渐变颜色效果 2023-01-16
 - Unity Shader实现模糊效果 2023-04-27
 - C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
 - Oracle中for循环的使用方法 2023-07-04
 - 如何使用C# 捕获进程输出 2023-03-10
 - .NET CORE DI 依赖注入 2023-09-27
 - c# 模拟线性回归的示例 2023-03-14
 
						
						
						
						
						
				
				
				
				