C#分享辅助类:图像笔刷(ImageBrushHandler)

该代码段展示了如何在WPF中将Bitmap、Stream、本地文件路径和网络URL转换为ImageBrush和ImageSource。GetImageBrush系列方法用于创建画笔,而GetImageSource系列方法则用于获取ImageSource对象,支持本地和网络资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

名称

方法

得到图像WPF画笔

GetImageBrush

得到本地图像画笔

GetLocalImageBrush

得到网络图像画笔

GetNetImageBrush

得到ImageSouce

GetImageSource

得到本地ImageSouce

GetLocalImageSource

得到网络ImageSouce

GetNetImageSource

得到图标ImageScouce

GetIconImageSouce

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

namespace Wsfly
{
    /// <summary>
    /// WPF图片处理
    /// </summary>
    public class ImageBrushHandler
    {
        #region 图像转画笔
        /// <summary>
        /// 得到图像WPF画笔
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static ImageBrush GetImageBrush(Bitmap bitmap)
        {
            ImageBrush brush = new ImageBrush();

            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                stream.Position = 0;
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();

                brush.ImageSource = result;
            }

            return brush;
        }
        /// <summary>
        /// 得到图像WPF画笔
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static ImageBrush GetImageBrush(Stream stream)
        {
            ImageBrush brush = new ImageBrush();

            using (stream)
            {
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
                brush.ImageSource = result;
            }

            return brush;
        }
        #endregion

        #region 图片路径转画笔
        /// <summary>
        /// 得到本地图像画笔
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ImageBrush GetLocalImageBrush(string path)
        {
            //不存在文件
            if (!System.IO.File.Exists(path)) return null;

            //转换为图像
            //var image = System.Drawing.Image.FromFile(path);
            //var bitmap = new System.Drawing.Bitmap(image);
            //var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
            //                                                                      IntPtr.Zero,
            //                                                                      System.Windows.Int32Rect.Empty,
            //                                                                      BitmapSizeOptions.FromEmptyOptions()
            //                    );
            //bitmap.Dispose();
            //image.Dispose();

            BitmapImage bitmapSource = new BitmapImage();
            bitmapSource.BeginInit();
            bitmapSource.CacheOption = BitmapCacheOption.OnLoad;
            bitmapSource.UriSource = new Uri(path);
            bitmapSource.EndInit();
            bitmapSource.Freeze();

            //得到画笔
            var brush = new ImageBrush(bitmapSource);

            return brush;
        }
        /// <summary>
        /// 得到网络图像画笔
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static ImageBrush GetNetImageBrush(string url)
        {
            try
            {
                //转换为图像
                //var image = WebHandler.GetImgFile(url);
                //var bitmap = new System.Drawing.Bitmap(image);
                //var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                //                                                                      IntPtr.Zero,
                //                                                                      System.Windows.Int32Rect.Empty,
                //                                                                      BitmapSizeOptions.FromEmptyOptions()
                //                    );
                //bitmap.Dispose();
                //image.Dispose();

                //得到图像
                var image = WebHandler.GetImgFile(url);
                if (image != null)
                {
                    MemoryStream stream = new MemoryStream();
                    image.Save(stream, ImageFormat.Png);
                    BitmapImage img = new BitmapImage();
                    using (stream)
                    {
                        img.BeginInit();
                        img.CacheOption = BitmapCacheOption.OnLoad;
                        img.StreamSource = stream;
                        img.EndInit();
                        img.Freeze();
                    }
                    return new ImageBrush(img);
                }
            }
            catch { }

            return null;
        }
        #endregion

        #region 得到ImageSouce
        /// <summary>
        /// 得到ImageSouce
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static ImageSource GetImageSource(Bitmap bitmap)
        {
            BitmapImage result = new BitmapImage();

            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                stream.Position = 0;
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
            }

            return result;
        }
        /// <summary>
        /// 得到ImageSouce
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static ImageSource GetImageSource(Stream stream)
        {
            BitmapImage result = new BitmapImage();

            using (stream)
            {
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
            }

            return result;
        }
        /// <summary>
        /// 得到本地ImageSouce
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ImageSource GetLocalImageSource(string path)
        {
            //不存在文件
            if (!System.IO.File.Exists(path)) return null;

            //转换为图像
            //var image = System.Drawing.Image.FromFile(path);
            //var bitmap = new System.Drawing.Bitmap(image);
            //var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
            //                                                                      IntPtr.Zero,
            //                                                                      System.Windows.Int32Rect.Empty,
            //                                                                      BitmapSizeOptions.FromEmptyOptions()
            //                    );
            释放资源
            //bitmap.Dispose();
            //image.Dispose();

            BitmapImage bitmapSource = new BitmapImage();
            bitmapSource.BeginInit();
            bitmapSource.CacheOption = BitmapCacheOption.OnLoad;
            bitmapSource.UriSource = new Uri(path);
            bitmapSource.EndInit();
            bitmapSource.Freeze();

            return bitmapSource;
        }
        /// <summary>
        /// 得到网络ImageSouce
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static ImageSource GetNetImageSource(string url)
        {
            try
            {
                //转换为图像
                //var image = WebHandler.GetImgFile(url);
                //var bitmap = new System.Drawing.Bitmap(image);
                //var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
                //                                                                      IntPtr.Zero,
                //                                                                      System.Windows.Int32Rect.Empty,
                //                                                                      BitmapSizeOptions.FromEmptyOptions()
                //                    );
                //bitmap.Dispose();
                //image.Dispose();

                var image = WebHandler.GetImgFile(url);
                if (image != null)
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        if (stream != null && stream.Length > 0)
                        {
                            return GetImageSource(stream);
                        }
                    }
                }
            }
            catch (Exception ex) { }

            return null;
        }
        /// <summary>
        /// 得到ImageSource
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static ImageSource GetIconImageSouce(Icon icon)
        {
            if (icon == null) return null;

            try
            {
                return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle,
                    System.Windows.Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }
            catch { }

            return null;
        }

        #endregion
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MOZ-Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值