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.
87 lines
2.3 KiB
87 lines
2.3 KiB
3 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
|
}
|