赞
踩
- public class ImageHelper
- {
- public static string SaveImage(string base64String,string imageDir)
- {
- string picPath = "";
-
- try
- {
- byte[] bytes = System.Convert.FromBase64String(base64String);//base64转为byte数组
- MemoryStream ms = new MemoryStream(bytes);//创建内存流,将图片编码导入
- Image image = Image.FromStream(ms);
-
- Random ran = new Random((int)DateTime.Now.Ticks);
- string fileName = DateTime.Now.ToString("yyyyMMddhhmmssms") + ran.Next() + ".jpeg";
- //文件保存位置及命名,精确到毫秒并附带一组随机数,防止文件重名,数据库保存路径为此变量
- string serverPath = imageDir + "\\" + fileName;
- string fullPath = (System.AppDomain.CurrentDomain.BaseDirectory + serverPath);
-
- ImageFormat imgfor = ImageFormat.Jpeg;//设置图片格式
- image.Save(fullPath, imgfor); //保存图片
-
- ms.Close();
- ms.Dispose();
- image.Dispose();
-
- picPath = imageDir + "/" + fileName;
- }
- catch (Exception err)
- {
-
- }
-
-
- return picPath;
- }
-
- public static byte[] GetImageAsByteArray(string imageFilePath)
- {
- using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
- {
- using (BinaryReader binaryReader = new BinaryReader(fileStream))
- {
- return binaryReader.ReadBytes((int)fileStream.Length);
- }
- }
- }
-
- public static string GetBase64String(byte[] data)
- {
- return Convert.ToBase64String(data);
- }
-
- /// <summary>
- /// 无损压缩图片
- /// </summary>
- /// <param name="sFile">原图片地址</param>
- /// <param name="dFile">压缩后保存图片地址</param>
- /// <param name="flag">压缩质量(数字越小压缩率越高)1-100</param>
- /// <param name="size">压缩后图片的最大大小</param>
- /// <param name="sfsc">是否是第一次调用</param>
- /// <returns></returns>
- public static bool CompressImage(string sFile, string dFile, int flag = 97, int size = 500, bool sfsc = true)
- {
- //如果是第一次调用,原始图像的大小小于要压缩的大小,则直接复制文件,并且返回true
- FileInfo firstFileInfo = new FileInfo(sFile);
- if (sfsc == true && firstFileInfo.Length < size * 1024)
- {
- firstFileInfo.CopyTo(dFile);
- return true;
- }
- Image iSource = Image.FromFile(sFile);
- ImageFormat tFormat = iSource.RawFormat;
- int dHeight, dWidth;
- if(iSource.Height > 1500 || iSource.Width > 1500)
- {
- dHeight = iSource.Height * 2 / 3;
- dWidth = iSource.Width * 2 / 3;
- }
- else
- {
- dHeight = iSource.Height;
- dWidth = iSource.Width;
- }
- int sW = 0, sH = 0;
- //按比例缩放
- Size tem_size = new Size(iSource.Width, iSource.Height);
- if (tem_size.Width > dHeight || tem_size.Width > dWidth)
- {
- if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
- {
- sW = dWidth;
- sH = (dWidth * tem_size.Height) / tem_size.Width;
- }
- else
- {
- sH = dHeight;
- sW = (tem_size.Width * dHeight) / tem_size.Height;
- }
- }
- else
- {
- sW = tem_size.Width;
- sH = tem_size.Height;
- }
-
- Bitmap ob = new Bitmap(dWidth, dHeight);
- Graphics g = Graphics.FromImage(ob);
-
- g.Clear(Color.WhiteSmoke);
- g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
-
- g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
-
- g.Dispose();
-
- //以下代码为保存图片时,设置压缩质量
- EncoderParameters ep = new EncoderParameters();
- long[] qy = new long[1];
- qy[0] = flag;//设置压缩的比例1-100
- EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
- ep.Param[0] = eParam;
-
- try
- {
- ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
- ImageCodecInfo jpegICIinfo = null;
- for (int x = 0; x < arrayICI.Length; x++)
- {
- if (arrayICI[x].FormatDescription.Equals("JPEG"))
- {
- jpegICIinfo = arrayICI[x];
- break;
- }
- }
- if (jpegICIinfo != null)
- {
- ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
- FileInfo fi = new FileInfo(dFile);
- if (fi.Length > 1024 * size)
- {
- flag = flag - 10;
- CompressImage(sFile, dFile, flag, size, false);
- }
- }
- else
- {
- ob.Save(dFile, tFormat);
- }
- return true;
- }
- catch
- {
- return false;
- }
- finally
- {
- iSource.Dispose();
- ob.Dispose();
- }
- }
-
- /// <summary>
- /// 根据路径删除文件
- /// </summary>
- /// <param name="path"></param>
- public static void DeleteFile(string path)
- {
- FileAttributes attr = File.GetAttributes(path);
- if (attr == FileAttributes.Directory)
- {
- Directory.Delete(path, true);
- }
- else
- {
- File.Delete(path);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。