【问题标题】:How to add one image as a frame (border) to another image in C#?如何将一个图像作为框架(边框)添加到 C# 中的另一个图像?
【发布时间】:2017-01-04 16:34:21
【问题描述】:

我有一些 jpeg 照片,需要在我的 WPF 应用程序中为每张照片放置一个框架。框架也是一个图像,像这样:

最好的方法是什么?

也许可以在画布上绘制图像,然后在其上方绘制一个框架?

或者也许有一种方法可以按原样显示图像,然后用边框将其框起来?

【问题讨论】:

  • 使用你喜欢的图像编辑器使框架的内部部分透明,然后将内部图像和框架图像放在一起的两个 Image 控件,例如通过将两者都放在一个网格中。

标签: c# wpf image


【解决方案1】:

将框架图像设为 .png 并使内部窗口透明。 WPF 会将其呈现为透明,因此您将其他图像置于其下不会有任何问题,并且该图像将通过框架图像的透明部分显示。

【讨论】:

    【解决方案2】:

    下面是代码,它将在照片上添加边框:

    附:也可以使用出色的 imageresizing.net 库及其 Watermark 插件。

    private void AddFrameAndSave(Image photo, Image frame, string outputFilePath)
    {
            using (frame)
            {
                var width = 600; //Set the final image width. Usually it'll be photo.width
                var height = 450; //The same with height
                using (var bitmap = new Bitmap(width, height))
                {
                    using (var canvas = Graphics.FromImage(bitmap))
                    {
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        canvas.DrawImage(photo,
                            new Rectangle(0,
                                0,
                                width,
                                height),
                            new Rectangle(0,
                                0,
                                photo.Width,
                                photo.Height),
                            GraphicsUnit.Pixel);
                        canvas.DrawImage(frame, new Rectangle(0,
                            0,
                            width,
                            height), new Rectangle(0,
                            0,
                            frame.Width,
                            frame.Height), GraphicsUnit.Pixel);
    
                        canvas.Save();
                    }
    
                    bitmap.Save(outputFilePath,ImageFormat.Jpeg);
    
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2020-02-26
      • 2015-01-31
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2013-02-02
      • 2018-06-25
      相关资源
      最近更新 更多