C# 从指定路径读取图片源、Bitmap与ImageSource互转、Bitmap与BitmapImage互转、BitmapImage转为byte[]、图片压缩

从指定路径读取图片源

public static BitmapImage LoadBitmapImageByPath(string path)
{
    try
    {
        //文件不存在,返回空
        if (!File.Exists(path))
        {
            return null;
        }
        BitmapImage bi = new BitmapImage();
        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                byte[] bytes = br.ReadBytes((int)stream.Length);
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(bytes);
                bi.EndInit();
            }
        }
        return bi;
    }
    catch (Exception ex)
    {
        return null;
    }
}

Bitmap与ImageSource互转、Bitmap与BitmapImage互转、BitmapImage转为byte[]、图片压缩


#region Bitmap与ImageSource互转
/// <summary>
/// Bitmap 转为ImageSource
/// </summary>
/// <param name="bitmap">Bitmap 对象</param>
/// <returns>ImageSource 位图对象</returns>
public static ImageSource BitmapToImageSource(System.Drawing.Bitmap bitmap)
{
    try
    {
        IntPtr intPtr = bitmap.GetHbitmap();
        ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtr,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
        return imageSource;
    }
    catch (Exception ex)
    {
        WriteLog.WriteErrorInfo(ex);
    }
    return null;
}

/// <summary>
/// ImageSource 转为Bitmap
/// </summary>
/// <param name="imageSource">imageSource 对象</param>
/// <returns>返回 Bitmap 对象</returns>
public static System.Drawing.Bitmap ImageSourceToBitmap(ImageSource imageSource)
{
    try
    {             
        BitmapSource bitmapSource = (BitmapSource)imageSource;
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size),System.Drawing.Imaging.ImageLockMode.WriteOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        bitmapSource.CopyPixels(Int32Rect.Empty,data.Scan0,data.Height*data.Stride,data.Stride);
        bitmap.UnlockBits(data);
        return bitmap;
    }
    catch (Exception ex)
    {
        WriteLog.WriteErrorInfo(ex);
    }
    return null;
}
#endregion

#region  Bitmap与BitmapImage互转
//将Bitmap对象转换成bitmapImage对象
public BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
    MemoryStream stream = new MemoryStream();
    bitmap.Save(stream, ImageFormat.Bmp);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}

//将bitmapImage对象转换成Bitmap对象
public static System.Drawing.Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
    using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapImage));
        enc.Save(outStream);
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
        return bitmap;
    }
}
#endregion

#region BitmapImage 转为byte[]
/// <summary>
/// BitmapImage 转为byte[]
/// </summary>
/// <param name="bitmapImage">BitmapImage 对象</param>
/// <returns>byte[] 数组</returns>
public static byte[] BitmapImageToByteArray(BitmapImage bitmapImage)
{
    byte[] buffer = new byte[] { };
    try
    {
        Stream stream = bitmapImage.StreamSource;
        if (stream!=null&& stream.Length>0)
        {
            stream.Position = 0;
            using (BinaryReader binary=new BinaryReader(stream))
            {
                buffer = binary.ReadBytes((int)stream.Length);
            }
        }
    }
    catch (Exception ex)
    {
        WriteLog.WriteErrorInfo(ex);
    }
    return buffer;
}
#endregion

#region 图片压缩
/// <summary>
/// 图片压缩
/// </summary>
/// <param name="bitmap">要压缩的源图像</param>
/// <param name="height">要求的高</param>
/// <param name="width">要求的宽</param>
/// <returns></returns>
public static System.Drawing.Bitmap GetPicThumbnail(System.Drawing.Bitmap bitmap , int height, int width)
{
    try
    {
        lock (bitmap)
        {
            System.Drawing.Bitmap iSource = bitmap;
            System.Drawing.Imaging.ImageFormat imageFormat = iSource.RawFormat;
            int sw = width, sh = height;
            //按比例缩放
            System.Drawing.Bitmap ob = new System.Drawing.Bitmap(width, height);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(ob);
            graphics.Clear(System.Drawing.Color.WhiteSmoke);
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(iSource,new System.Drawing.Rectangle((width-sw)/2,(height-sh)/2,sw,sh),0,0,iSource.Width,iSource.Height,System.Drawing.GraphicsUnit.Pixel);
            graphics.Dispose();
            return ob;
        }
    }
    catch (Exception ex)
    {
        WriteLog.WriteErrorInfo(ex);
    }
    return bitmap;
}
#endregion

参考链接:

https://www.cnblogs.com/lipengxu/p/15734297.html

https://blog.csdn.net/xpj8888/article/details/88351176

.... 

 

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wangnaisheng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值