[c#]代码库
public static string ImageFile2Base64(string filename)
{
if (!File.Exists(filename))
return String.Empty;
System.Drawing.Image image = System.Drawing.Image.FromFile(filename);
return Image2Base64(image);
}
public static void Base642ImageFile(string base64, string filename)
{
try
{
System.Drawing.Image image = Base642Image(base64);
System.Drawing.Bitmap newimage = new System.Drawing.Bitmap(image);
image.Dispose();
string dir = Path.GetDirectoryName(filename);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
newimage.Save(filename);
}
catch (Exception exc)
{ }
}
public static void Base642ImageFile(string base64, string filename, System.Drawing.Imaging.ImageFormat format)
{
System.Drawing.Image image = Base642Image(base64);
image.Save(filename, format);
}
public static string Image2Base64(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Byte[] filedata = ms.ToArray();
ms.Close();
return Convert.ToBase64String(filedata);
}
public static System.Drawing.Image Base642Image(string base64)
{
Byte[] filedate = Convert.FromBase64String(base64);
MemoryStream ms = new MemoryStream(filedate);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
ms.Flush();
ms.Close();
System.Drawing.Image newimage = image.Clone() as System.Drawing.Image;
image.Dispose();
return newimage;
}
by: 发表于:2017-12-20 17:29:27 顶(0) | 踩(0) 回复
??
回复评论