You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

86 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
namespace Stone.Common
{
public class MyImage
{
/// <summary>
/// 图片转换为byte[]
/// </summary>
/// <param name="Image"></param>
/// <param name="imageFormat"></param>
/// <returns></returns>
public static byte[] ImageToBuffer(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
{
if (Image == null) { return null; }
byte[] data = null;
using (MemoryStream oMemoryStream = new MemoryStream())
{
using (Bitmap oBitmap = new Bitmap(Image))
{
oBitmap.Save(oMemoryStream, imageFormat);
oMemoryStream.Position = 0;
data = new byte[oMemoryStream.Length];
oMemoryStream.Read(data, 0, Convert.ToInt32(oMemoryStream.Length));
oMemoryStream.Flush();
}
}
return data;
}
/// <summary>
/// byte[]转换为图片
/// </summary>
/// <param name="Buffer"></param>
/// <returns></returns>
public static Image BufferToImage(byte[] Buffer)
{
if (Buffer == null || Buffer.Length == 0) { return null; }
byte[] data = null;
Image oImage = null;
Bitmap oBitmap = null;
data = (byte[])Buffer.Clone();
try
{
MemoryStream oMemoryStream = new MemoryStream(Buffer);
oMemoryStream.Position = 0;
oImage = System.Drawing.Image.FromStream(oMemoryStream);
oBitmap = new Bitmap(oImage);
}
catch
{
throw;
}
return oBitmap;
}
/// <summary>
/// 从文件读取图片到Image中(以非独占的方式)
/// </summary>
/// <param name="FileName"></param>
/// <returns></returns>
public static Image ReadImageFile(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();
return image;
}
}
}