using System.Drawing;
using System.IO;
#region 将BiteMapImage 转成byte[]
/// <summary>
/// 将BiteMapImage 转成byte[]
/// </summary>
/// <param name="bmp">BitmapImage</param>
/// <returns>byte[]</returns>
public static byte[] BitMapImageToByteArray(BitmapImage bmp)
{
if (bmp == null) return null;
Stream smarket = bmp.StreamSource;
if (smarket == null || smarket.Length == 0) return null;
byte[] bytearray = null;
CatchException(() =>
{
//设置当前位置
smarket.Position = 0;
using (BinaryReader br = new BinaryReader(smarket))
bytearray = br.ReadBytes((int)smarket.Length);
});
return bytearray;
}
#endregion
#region 将BitMapSource 转成 BitMapImage
/// <summary>
/// 将BitMapSoruce 转成 BitMapImage
/// </summary>
/// <param name="source">BitmapSource</param>
/// <returns>BitmapImage</returns>
public static BitmapImage BitMapSoruceToBitMapImage(BitmapSource source)
{
if (source == null) return null;
BitmapImage bImg = null;
CatchException(() =>
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
bImg = new BitmapImage();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(memoryStream);
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(memoryStream.ToArray());
bImg.EndInit();
memoryStream.Close();
});
return bImg;
}
#endregion
#region 将byte[]转成Bitmap
/// <summary>
/// byte[]转成Bitmap
/// </summary>
/// <param name="bytes">byte[]</param>
/// <returns>Bitmap</returns>
public static Bitmap ConvertByteToImg(byte[] bytes)
{
if (bytes == null || bytes.Length == 0) return null;
Bitmap img = null;
CatchException(() =>
{
/*
* 使用using(MemoryStream ms = new MemoryStream(bytes)){img = new Bitmap(ms);}
* 出现 Method 异常对象名称: Int32 SelectActiveFrame(System.Drawing.Imaging.FrameDimension, Int32)异常
*/
MemoryStream ms = new MemoryStream(bytes);
//{
ms.Seek(0, SeekOrigin.Begin);
img = new Bitmap(ms);
//}
});
return img;
}
#endregion
#region 将byte[]转换为Zip压缩包
/// <summary>
/// 将byte[]转换为Zip压缩包
/// </summary>
/// <param name="zipByte">文件二进制字节数组</param>
/// <param name="zipName">文件包名称</param>
/// <returns>Task</returns>
public static async Task ConvertByteToZip(byte[] zipByte, string zipName)
{
if (zipByte == null || zipByte.Length == 0) return;
await Task.Run(() =>
{
CatchException(() =>
{
string fileName = $"{zipName}.zip";
string downLoadPath = $"{AppDomain.CurrentDomain.BaseDirectory}DownFiles\\";
string downLoadFilePath = downLoadPath;
if (!Directory.Exists(downLoadPath))
Directory.CreateDirectory(downLoadPath);
downLoadPath = downLoadPath + fileName;
using (FileStream fs = new FileStream(downLoadPath, FileMode.Create, FileAccess.Write))
fs.Write(zipByte, 0, zipByte.Length);
Process.Start(downLoadFilePath);
});
});
}
#endregion
#region 文件转成字节
/// <summary>
/// 文件转成字节
/// </summary>
/// <param name="fullpath">文件路径</param>
/// <returns>byte[]</returns>
public static byte[] ConvertFileToByte(string fullpath)
{
if (VerifyHelper.IsEmptyOrNullOrWhiteSpace(fullpath) || File.Exists(fullpath) == false) return null;
byte[] imagebytes = null;
CatchException(() =>
{
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
using (BinaryReader br = new BinaryReader(fs))
{
imagebytes = new byte[fs.Length];
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
}
});
return imagebytes;
}
#endregion
#region 图片转成字节数组
/// <summary>
/// Image转成字节
/// </summary>
/// <param name="pictureEdit">System.Drawing.Image 控件</param>
/// <returns>byte[]</returns>
public static byte[] ConvertImgToByte(Image image)
{
if (image == null) return null;
byte[] imgBytes = null;
CatchException(() =>
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
imgBytes = new byte[ms.Length];
ms.Position = 0;
ms.ReadAsync(imgBytes, 0, Convert.ToInt32(ms.Length));
}
});
return imgBytes;
}
#endregion