保存WPF位图效果结果图像

发布于 2024-10-07 03:09:11 字数 62 浏览 2 评论 0原文

我有一个要在页面中显示的图像,然后对图像应用一些视觉效果。例如斜角效果。成功应用效果后,是否可以保存结果图像?

I have a image to be displayed in the page and then apply some visual effects to the image. e.g Bevel effect. Once the effect is applied successfully is there a way to save the resultant image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2024-10-14 03:09:11

你可以做这样的事情

private void SaveImage()
    {
        try
        {

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "JPeg Image(*.JPG)|*.jpg|Bitmap Image(*.BMP)|*.bmp|Png Image(*.PNG)|*.png|Gif Image(*.GIF)|*.gif";

            if (saveDialog.ShowDialog().Value == true)
            {
                // Save current canvas transform
                Transform transform = img.LayoutTransform;
                // reset current transform (in case it is scaled or rotated)
                img.LayoutTransform = null;

                // Get the size of canvas
                Size size = new Size(img.ActualWidth, img.ActualHeight);
                // Measure and arrange the surface
                // VERY IMPORTANT
                img.Measure(size);
                img.Arrange(new Rect(size));

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Default);
                renderBitmap.Render(img);
                BitmapEncoder encoder = new BmpBitmapEncoder();
                string extension = saveDialog.FileName.Substring(saveDialog.FileName.LastIndexOf('.'));
                switch (extension.ToLower())
                {
                    case ".jpg":
                        encoder = new JpegBitmapEncoder();
                        break;
                    case ".bmp":
                        encoder = new BmpBitmapEncoder();
                        break;
                    case ".gif":
                        encoder = new GifBitmapEncoder();
                        break;
                    case ".png":
                        encoder = new PngBitmapEncoder();
                        break;
                }
                // push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                // Create a file stream for saving image
                using (FileStream fs = File.Open(saveDialog.FileName, FileMode.OpenOrCreate))
                {
                    encoder.Save(fs);
                }
                // Restore previously saved layout
                img.LayoutTransform = transform;
            }

        }
        catch (Exception)
        {

            throw;
        }
    }

you can do something like this

private void SaveImage()
    {
        try
        {

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "JPeg Image(*.JPG)|*.jpg|Bitmap Image(*.BMP)|*.bmp|Png Image(*.PNG)|*.png|Gif Image(*.GIF)|*.gif";

            if (saveDialog.ShowDialog().Value == true)
            {
                // Save current canvas transform
                Transform transform = img.LayoutTransform;
                // reset current transform (in case it is scaled or rotated)
                img.LayoutTransform = null;

                // Get the size of canvas
                Size size = new Size(img.ActualWidth, img.ActualHeight);
                // Measure and arrange the surface
                // VERY IMPORTANT
                img.Measure(size);
                img.Arrange(new Rect(size));

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Default);
                renderBitmap.Render(img);
                BitmapEncoder encoder = new BmpBitmapEncoder();
                string extension = saveDialog.FileName.Substring(saveDialog.FileName.LastIndexOf('.'));
                switch (extension.ToLower())
                {
                    case ".jpg":
                        encoder = new JpegBitmapEncoder();
                        break;
                    case ".bmp":
                        encoder = new BmpBitmapEncoder();
                        break;
                    case ".gif":
                        encoder = new GifBitmapEncoder();
                        break;
                    case ".png":
                        encoder = new PngBitmapEncoder();
                        break;
                }
                // push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                // Create a file stream for saving image
                using (FileStream fs = File.Open(saveDialog.FileName, FileMode.OpenOrCreate))
                {
                    encoder.Save(fs);
                }
                // Restore previously saved layout
                img.LayoutTransform = transform;
            }

        }
        catch (Exception)
        {

            throw;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文