csharp WPF为元素生成图片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WPF为元素生成图片相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Interop;
using System.Windows.Media;
using System.IO;

namespace Wind.Windows.Media.Imaging
{
    public static class ExportHelper
    {
        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        private static Matrix TransformToDevice
        {
            get
            {
                return PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformFromDevice;
            }
        }


        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        private static Matrix TransformToDeviceMatrix
        {
            get
            {
                if (!BrowserInteropHelper.IsBrowserHosted && ((Application.Current != null) && (Application.Current.MainWindow != null)))
                {
                    return TransformToDevice;
                }

                return Matrix.Identity;
            }
        }

        internal static double SystemDpiX
        {
            get
            {
                return (TransformToDeviceMatrix.M11 * 96.0);
            }
        }


        internal static double SystemDpiY
        {
            get
            {
                return (TransformToDeviceMatrix.M22 * 96.0);
            }
        }


        private static BitmapSource GetBitmapSource(UIElement element, double dpiX, double dpiY)
        {
            Size imageSize = GetImageSize(element);

            double num = dpiX / 96.0;
            double num2 = dpiY / 96.0;
            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(imageSize.Width * num), (int)(imageSize.Height * num2), dpiX, dpiY, PixelFormats.Default);


            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(element);
                dc.DrawRectangle(vb, null, new Rect(new Point(), imageSize));
            }

            bitmap.Render(dv);


            return bitmap;
        }




        internal static Size GetImageSize(UIElement element)
        {
            return new Size((int)element.RenderSize.Width, (int)element.RenderSize.Height);
        }



        public static BitmapSource GetElementImage(UIElement element)
        {
            return GetBitmapSource(element, SystemDpiX, SystemDpiY);
        }

        public static void BitmapSourceToStream(BitmapSource source, Stream stream,ExportImageType type)
        {
            BitmapEncoder encoder = BitmapEncoder.Create(BitmapEncoderHelper.GetImageTypeGuid(type));
            encoder.Frames.Add(BitmapFrame.Create(source));

            encoder.Save(stream);
        }

        public static void GetElementImageToStream(UIElement element, Stream stream, ExportImageType type)
        {
            BitmapSource image = GetBitmapSource(element, SystemDpiX, SystemDpiY);
            BitmapSourceToStream(image, stream, type);
        }


    }
}

以上是关于csharp WPF为元素生成图片的主要内容,如果未能解决你的问题,请参考以下文章

OxyPlot 导出图片及 WPF 元素导出为图片的方法

csharp 在c#中拖动wpf画布上的元素

急求:如何将 WPF ChartControl生成的图片保存到本地为jpg格式

WPF-动态生成DataGrid

csharp 内容生成器的示例代码,用于将子文本元素添加到当前元素

WPF C# 媒体元素显示视频的第一张图片