图片处理的基本方法,在web或者Form,简单总结,代码如下:
/// <summary>
/// base64字符串转换二进制
/// </summary>
public byte[] Base64Tbyte(string imagecode)
{
byte[] result = null;
result = Convert.FromBase64String(imagecode);
return result;
}
public byte[] ImageToByte(string path)
{
// 以二进制方式读文件
FileStream fsMyfile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// 创建一个二进制数据流读入器,和打开的文件关联
BinaryReader brMyfile = new BinaryReader(fsMyfile);
// 把文件指针重新定位到文件的开始
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString()));
// 关闭以上new的各个对象
brMyfile.Close();
return bytes;
}
public static byte[] Getbyte(Image img)
{
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
byte[] mydata = new byte[stream.Length];
mydata = stream.ToArray();
stream.Close();
return mydata;
}
public Image RetImage(byte[] imbyte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(imbyte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}