private int GetPictureFormat() { byte[] msgData = new byte[] { 0x03, 0x01, (byte)MessageSet.PictureFormatGet, 0x00 }; byte[] msgReport = null; if (this.SendMessage(msgData, ref msgReport) == 0) { if (msgReport[0] == (byte)MessageSet.PictureFormatSet) { this.pictureFormat = (PictureFormat)(msgReport[1] & 0x03); return(0); } else { return(2); } } else { return(1); } }
/// <summary> /// 转换图片格式 /// </summary> /// <param name="originalStream">源图片流</param> /// <param name="newFormat">图片输出格式</param> /// <returns>新图片流</returns> public static Stream FormatPicture(Stream originalStream, PictureFormat newFormat) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalStream); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(newFormat); System.IO.MemoryStream newStream = new MemoryStream(); try { //以指定格式保存图片 string outputFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png"; originalImage.Save(outputFile, imageFormat); FileStream fs = File.Open(outputFile, FileMode.Open); newStream = new MemoryStream(ConvertStream.ToBuffer(fs)); fs.Close(); File.Delete(outputFile); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); } return(newStream); }
internal static void TakeScreenshot(BitmapImage bitmap, PictureFormat format, string filename, int quality) { BitmapEncoder encoder; switch (format) { case PictureFormat.Jpg: filename += ".jpg"; encoder = new JpegBitmapEncoder { QualityLevel = quality }; break; case PictureFormat.Bmp: filename += ".bmp"; encoder = new BmpBitmapEncoder(); break; default: throw new ArgumentException($"Unknown format {nameof(format)}"); } using (var fs = new FileStream(filename, FileMode.OpenOrCreate)) { encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(fs); encoder.Frames.Clear(); } }
/// <summary> /// 转换图片格式 /// </summary> /// <param name="originalBase64">源图片Base64</param> /// <param name="newFormat">图片输出格式</param> /// <returns>新图片流</returns> public static Stream FormatPicture(string originalBase64, PictureFormat newFormat) { char[] charBuffer = originalBase64.ToCharArray(); byte[] bytes = Convert.FromBase64CharArray(charBuffer, 0, charBuffer.Length); System.Drawing.Image originalImage = System.Drawing.Image.FromStream(new MemoryStream(bytes)); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(newFormat); System.IO.MemoryStream newStream = new MemoryStream(); try { //以指定格式保存图片 string outputFile = Path.GetTempPath() + Guid.NewGuid().ToString() + "." + newFormat.ToString(); originalImage.Save(outputFile, imageFormat); FileStream fs = File.Open(outputFile, FileMode.Open); newStream = new MemoryStream(ConvertStream.ToBuffer(fs)); fs.Close(); File.Delete(outputFile); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); } return(newStream); }
private static void ProcessPictureFormat(PictureFormat pf, XmlWriter xmlw) { xmlw.WriteStartElement("PictureFormat"); xmlw.WriteAttributeString("BottomCropping", pf.BottomCropping.ToStringSafe()); xmlw.WriteAttributeString("LeftCropping", pf.LeftCropping.ToStringSafe()); xmlw.WriteAttributeString("RightCropping", pf.RightCropping.ToStringSafe()); xmlw.WriteAttributeString("TopCropping", pf.TopCropping.ToStringSafe()); xmlw.WriteEndElement(); }
private static void ProcessPictureFormat(PictureFormat pf, Utf8JsonWriter jsonw) { jsonw.WritePropertyName("PictureFormat"); jsonw.WriteStartObject(); jsonw.WriteString("BottomCropping", pf.BottomCropping.ToStringSafe()); jsonw.WriteString("LeftCropping", pf.LeftCropping.ToStringSafe()); jsonw.WriteString("RightCropping", pf.RightCropping.ToStringSafe()); jsonw.WriteString("TopCropping", pf.TopCropping.ToStringSafe()); jsonw.WriteEndObject(); }
void SavePreviewMedia() { if (mediaInPreview == MediaType.Picture) { PictureFormat pictureFormat = cameraRecorder.PictureFormat; mediaSaver.SavePicture(lastPictureTaken, pictureFormat); } else { VideoFormat videoFormat = cameraRecorder.VideoFormat; mediaSaver.SaveVideo(lastVideoPath, videoFormat); } }
public void SavePicture(Texture2D picture, PictureFormat pictureFormat) { string pictureName = GenerateMediaName(picturesBaseName, GetPictureFormatName(pictureFormat)); if (!Application.isEditor) { NativeGallery.SaveImageToGallery(picture, mediaPath, pictureName); } else { SavePictureForEditor(picture, pictureFormat, pictureName); } }
/// <summary>Updates the contents of this image to the given texture. /// Does nothing if the texture hasn't changed.</summary> public void UpdateImage(Texture image) { if (Image == null) { SetImage(image); return; } // Get the picture format: PictureFormat picture = Image.Contents as PictureFormat; // Check if it changed: if (picture == null || picture.Image != image) { SetImage(image); } }
/// <summary> /// 获取ImageFormat /// </summary> /// <param name="format"></param> /// <returns></returns> private static System.Drawing.Imaging.ImageFormat GetImageFormat(PictureFormat format) { if (format == PictureFormat.BMP) { return(System.Drawing.Imaging.ImageFormat.Bmp); } if (format == PictureFormat.GIF) { return(System.Drawing.Imaging.ImageFormat.Gif); } if (format == PictureFormat.JPG) { return(System.Drawing.Imaging.ImageFormat.Jpeg); } if (format == PictureFormat.PNG) { return(System.Drawing.Imaging.ImageFormat.Png); } return(System.Drawing.Imaging.ImageFormat.Bmp); }
// METHODS: #region public Bitmap TakeScreenShot() { screenShot.CopyFromScreen(0, 0, 0, 0, screenSize, CopyPixelOperation.SourceCopy); string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString(); string day = DateTime.Now.Day.ToString(); string hour = DateTime.Now.TimeOfDay.Hours.ToString(); string minute = DateTime.Now.TimeOfDay.Minutes.ToString(); string second = DateTime.Now.TimeOfDay.Seconds.ToString(); string path = SaveFolder + "\\image_" + day + "_" + month + "_" + year + "_" + hour + "_" + minute + "_" + second + "_." + PictureFormat.ToString(); if (PictureFormat == ImageFormat.Jpeg) { ChangeImageQuality(bmp, ImageQuality, path); } else { bmp.Save(path); } return(bmp); }
private int SetPictureFormat(PictureFormat pictureFormat) { byte[] msgData = new byte[] { 0x04, 0x01, (byte)MessageSet.PictureFormatSet, (byte)pictureFormat, 0x00 }; byte[] responseData = null; if (this.SendMessage(msgData, ref responseData) == 0) { Thread.Sleep(250); return(0); } else { return(1); } }
void SavePictureForEditor(Texture2D picture, PictureFormat pictureFormat, string pictureName) { byte[] imageData = null; string imagePath = Path.Combine(mediaPath, pictureName); switch (pictureFormat) { case PictureFormat.JPG: imageData = picture.EncodeToJPG(); break; case PictureFormat.PNG: imageData = picture.EncodeToPNG(); break; default: break; } if (imageData != null) { File.WriteAllBytes(imagePath, imageData); } }
internal override bool NowOnScreen() { if (Image == null || !Image.Loaded) { // Reject the visibility state change. ImageLocation = null; return(false); } // Tell it that it's going on screen: Image.GoingOnDisplay(RenderData); // Must be PictureFormat to go onto the atlas: PictureFormat picture = Image.Contents as PictureFormat; if (picture == null) { // Reject the visibility state change (only available to images). ImageLocation = null; return(false); } if (Isolated) { if (ImageLocation != null) { ImageLocation.DecreaseUsage(); ImageLocation = null; } return(true); } ImageLocation = RequireImage(Image); return(true); }
private void CalculateImageDimensions() { ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo; if (formatInfo.failure == ImageFailure.None) { XImage xImage = null; try { xImage = XImage.FromFile(this.imageFilePath); } catch (InvalidOperationException ex) { Trace.WriteLine(Messages.InvalidImageType(ex.Message)); formatInfo.failure = ImageFailure.InvalidType; } try { XUnit usrWidth = image.Width.Point; XUnit usrHeight = image.Height.Point; bool usrWidthSet = !this.image.IsNull("Width"); bool usrHeightSet = !this.image.IsNull("Height"); XUnit resultWidth = usrWidth; XUnit resultHeight = usrHeight; double xPixels = xImage.PixelWidth; bool usrResolutionSet = !image.IsNull("Resolution"); double horzRes = usrResolutionSet ? (double)image.Resolution : xImage.HorizontalResolution; XUnit inherentWidth = XUnit.FromInch(xPixels / horzRes); double yPixels = xImage.PixelHeight; double vertRes = usrResolutionSet ? (double)image.Resolution : xImage.VerticalResolution; XUnit inherentHeight = XUnit.FromInch(yPixels / vertRes); bool lockRatio = this.image.IsNull("LockAspectRatio") ? true : image.LockAspectRatio; double scaleHeight = this.image.ScaleHeight; double scaleWidth = this.image.ScaleWidth; bool scaleHeightSet = !this.image.IsNull("ScaleHeight"); bool scaleWidthSet = !this.image.IsNull("ScaleWidth"); if (lockRatio && !(scaleHeightSet && scaleWidthSet)) { if (usrWidthSet && !usrHeightSet) { resultHeight = inherentHeight / inherentWidth * usrWidth; } else if (usrHeightSet && !usrWidthSet) { resultWidth = inherentWidth / inherentHeight * usrHeight; } else if (!usrHeightSet && !usrWidthSet) { resultHeight = inherentHeight; resultWidth = inherentWidth; } if (scaleHeightSet) { resultHeight = resultHeight * scaleHeight; resultWidth = resultWidth * scaleHeight; } if (scaleWidthSet) { resultHeight = resultHeight * scaleWidth; resultWidth = resultWidth * scaleWidth; } } else { if (!usrHeightSet) { resultHeight = inherentHeight; } if (!usrWidthSet) { resultWidth = inherentWidth; } if (scaleHeightSet) { resultHeight = resultHeight * scaleHeight; } if (scaleWidthSet) { resultWidth = resultWidth * scaleWidth; } } formatInfo.CropWidth = (int)xPixels; formatInfo.CropHeight = (int)yPixels; if (!this.image.IsNull("PictureFormat")) { PictureFormat picFormat = this.image.PictureFormat; //Cropping in pixels. XUnit cropLeft = picFormat.CropLeft.Point; XUnit cropRight = picFormat.CropRight.Point; XUnit cropTop = picFormat.CropTop.Point; XUnit cropBottom = picFormat.CropBottom.Point; formatInfo.CropX = (int)(horzRes * cropLeft.Inch); formatInfo.CropY = (int)(vertRes * cropTop.Inch); formatInfo.CropWidth -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch); formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch); //Scaled cropping of the height and width. double xScale = resultWidth / inherentWidth; double yScale = resultHeight / inherentHeight; cropLeft = xScale * cropLeft; cropRight = xScale * cropRight; cropTop = yScale * cropTop; cropBottom = yScale * cropBottom; resultHeight = resultHeight - cropTop - cropBottom; resultWidth = resultWidth - cropLeft - cropRight; } if (resultHeight <= 0 || resultWidth <= 0) { formatInfo.Width = XUnit.FromCentimeter(2.5); formatInfo.Height = XUnit.FromCentimeter(2.5); Trace.WriteLine(Messages.EmptyImageSize); this.failure = ImageFailure.EmptySize; } else { formatInfo.Width = resultWidth; formatInfo.Height = resultHeight; } } catch (Exception ex) { Trace.WriteLine(Messages.ImageNotReadable(this.image.Name, ex.Message)); formatInfo.failure = ImageFailure.NotRead; } finally { if (xImage != null) { xImage.Dispose(); } } } if (formatInfo.failure != ImageFailure.None) { if (!this.image.IsNull("Width")) { formatInfo.Width = this.image.Width.Point; } else { formatInfo.Width = XUnit.FromCentimeter(2.5); } if (!this.image.IsNull("Height")) { formatInfo.Height = this.image.Height.Point; } else { formatInfo.Height = XUnit.FromCentimeter(2.5); } return; } }
private void CalculateImageDimensions() { ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo; if (formatInfo.Failure == ImageFailure.None) { XImage xImage = null; try { //xImage = XImage.FromFile(_imageFilePath); xImage = CreateXImage(_imageFilePath); } catch (InvalidOperationException ex) { Debug.WriteLine(Messages2.InvalidImageType(ex.Message)); formatInfo.Failure = ImageFailure.InvalidType; } if (formatInfo.Failure == ImageFailure.None) { try { XUnit usrWidth = _image.Width.Point; XUnit usrHeight = _image.Height.Point; bool usrWidthSet = !_image._width.IsNull; bool usrHeightSet = !_image._height.IsNull; XUnit resultWidth = usrWidth; XUnit resultHeight = usrHeight; Debug.Assert(xImage != null); double xPixels = xImage.PixelWidth; bool usrResolutionSet = !_image._resolution.IsNull; double horzRes = usrResolutionSet ? _image.Resolution : xImage.HorizontalResolution; double vertRes = usrResolutionSet ? _image.Resolution : xImage.VerticalResolution; // ReSharper disable CompareOfFloatsByEqualityOperator if (horzRes == 0 && vertRes == 0) { horzRes = 72; vertRes = 72; } else if (horzRes == 0) { Debug.Assert(false, "How can this be?"); horzRes = 72; } else if (vertRes == 0) { Debug.Assert(false, "How can this be?"); vertRes = 72; } // ReSharper restore CompareOfFloatsByEqualityOperator XUnit inherentWidth = XUnit.FromInch(xPixels / horzRes); double yPixels = xImage.PixelHeight; XUnit inherentHeight = XUnit.FromInch(yPixels / vertRes); //bool lockRatio = _image.IsNull("LockAspectRatio") ? true : _image.LockAspectRatio; bool lockRatio = _image._lockAspectRatio.IsNull || _image.LockAspectRatio; double scaleHeight = _image.ScaleHeight; double scaleWidth = _image.ScaleWidth; //bool scaleHeightSet = !_image.IsNull("ScaleHeight"); //bool scaleWidthSet = !_image.IsNull("ScaleWidth"); bool scaleHeightSet = !_image._scaleHeight.IsNull; bool scaleWidthSet = !_image._scaleWidth.IsNull; if (lockRatio && !(scaleHeightSet && scaleWidthSet)) { if (usrWidthSet && !usrHeightSet) { resultHeight = inherentHeight / inherentWidth * usrWidth; } else if (usrHeightSet && !usrWidthSet) { resultWidth = inherentWidth / inherentHeight * usrHeight; } // ReSharper disable once ConditionIsAlwaysTrueOrFalse else if (!usrHeightSet && !usrWidthSet) { resultHeight = inherentHeight; resultWidth = inherentWidth; } if (scaleHeightSet) { resultHeight = resultHeight * scaleHeight; resultWidth = resultWidth * scaleHeight; } if (scaleWidthSet) { resultHeight = resultHeight * scaleWidth; resultWidth = resultWidth * scaleWidth; } } else { if (!usrHeightSet) { resultHeight = inherentHeight; } if (!usrWidthSet) { resultWidth = inherentWidth; } if (scaleHeightSet) { resultHeight = resultHeight * scaleHeight; } if (scaleWidthSet) { resultWidth = resultWidth * scaleWidth; } } formatInfo.CropWidth = (int)xPixels; formatInfo.CropHeight = (int)yPixels; if (_image._pictureFormat != null && !_image._pictureFormat.IsNull()) { PictureFormat picFormat = _image.PictureFormat; //Cropping in pixels. XUnit cropLeft = picFormat.CropLeft.Point; XUnit cropRight = picFormat.CropRight.Point; XUnit cropTop = picFormat.CropTop.Point; XUnit cropBottom = picFormat.CropBottom.Point; formatInfo.CropX = (int)(horzRes * cropLeft.Inch); formatInfo.CropY = (int)(vertRes * cropTop.Inch); formatInfo.CropWidth -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch); formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch); //Scaled cropping of the height and width. double xScale = resultWidth / inherentWidth; double yScale = resultHeight / inherentHeight; cropLeft = xScale * cropLeft; cropRight = xScale * cropRight; cropTop = yScale * cropTop; cropBottom = yScale * cropBottom; resultHeight = resultHeight - cropTop - cropBottom; resultWidth = resultWidth - cropLeft - cropRight; } if (resultHeight <= 0 || resultWidth <= 0) { formatInfo.Width = XUnit.FromCentimeter(2.5); formatInfo.Height = XUnit.FromCentimeter(2.5); Debug.WriteLine(Messages2.EmptyImageSize); _failure = ImageFailure.EmptySize; } else { formatInfo.Width = resultWidth; formatInfo.Height = resultHeight; } } catch (Exception ex) { Debug.WriteLine(Messages2.ImageNotReadable(_image.Name, ex.Message)); formatInfo.Failure = ImageFailure.NotRead; } finally { if (xImage != null) { xImage.Dispose(); } } } } if (formatInfo.Failure != ImageFailure.None) { if (!_image._width.IsNull) { formatInfo.Width = _image.Width.Point; } else { formatInfo.Width = XUnit.FromCentimeter(2.5); } if (!_image._height.IsNull) { formatInfo.Height = _image.Height.Point; } else { formatInfo.Height = XUnit.FromCentimeter(2.5); } } }
string GetPictureFormatName(PictureFormat pictureFormat) { return("." + pictureFormat.ToString().ToLower()); }
/// <summary> /// 转换图片格式 /// </summary> /// <param name="originalStream">源图片流</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="newFormat">图片输出格式</param> /// <returns>新图片流</returns> public static Stream FormatPicture(Stream originalStream, int formatWidth, int formatHeight, bool cut, PictureFormat newFormat) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalStream); if (formatWidth > 0 && formatHeight == 0) { formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width)); } if (formatHeight > 0 && formatWidth == 0) { formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height)); } int towidth = formatWidth; int toheight = formatHeight; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (cut) { //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * formatHeight / towidth; x = 0; y = (originalImage.Height - oh) / 2; } } else { //默认指定高度宽度,失真缩放 //指定宽度缩放 if (formatHeight <= 0) { formatWidth = originalImage.Width * formatHeight / originalImage.Height; } //指定高度缩放 else if (formatWidth <= 0) { formatHeight = originalImage.Height * formatWidth / originalImage.Width; } } //新建一个bmp图片 System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); //新建一个画板 System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //设置高质量插值法 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage( originalImage, new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel ); originalImage.Dispose(); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(newFormat); MemoryStream newStream = new MemoryStream(); try { //以指定格式保存图片 string outputFile = Path.GetTempPath() + Guid.NewGuid().ToString();// +".jpg"; image.Save(outputFile, imageFormat); FileStream fs = File.Open(outputFile, FileMode.Open); newStream = new MemoryStream(ConvertStream.ToBuffer(fs)); fs.Close(); File.Delete(outputFile); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); } return(newStream); }
private void FormattingPicture(int command) { Format = (PictureFormat)(command); }
/// <summary> /// 格式化图片 /// </summary> /// <param name="fileBinary">源图片二进制</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="picFormat">图片输出格式</param> /// <returns>新图片二进制</returns> public static Byte[] FormatPicture(Byte[] fileBinary, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(new MemoryStream(fileBinary)); if (formatWidth > 0 && formatHeight == 0) { formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width)); } if (formatHeight > 0 && formatWidth == 0) { formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height)); } int towidth = formatWidth; int toheight = formatHeight; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; if (cut) { //指定高宽裁减(不变形) if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * formatHeight / towidth; x = 0; y = (originalImage.Height - oh) / 2; } } else { //默认指定高度宽度,失真缩放 //指定宽度缩放 if (formatHeight <= 0) { formatWidth = originalImage.Width * formatHeight / originalImage.Height; } //指定高度缩放 else if (formatWidth <= 0) { formatHeight = originalImage.Height * formatWidth / originalImage.Width; } } //新建一个bmp图片 System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); //新建一个画板 System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); //设置高质量插值法 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 graphics.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 graphics.DrawImage( originalImage, new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel ); originalImage.Dispose(); System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(picFormat); //以指定格式保存图片 try { MemoryStream stream = new MemoryStream(); image.Save(stream, imageFormat); return(stream.GetBuffer()); } catch (Exception ex) { throw ex; } finally { //originalImage.Dispose(); image.Dispose(); graphics.Dispose(); } }
/* * * /// <summary> * /// 格式化图片 * /// </summary> * /// <param name="originalImagePath">源图物理路径</param> * /// <param name="formatImagePath">新图物理路径</param> * /// <param name="formatWidth">格式化宽度</param> * /// <param name="formatHeight">格式化高度</param> * /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> * /// <param name="picFormat">图片输出格式</param> * public static void FormatPicture(string originalPath, string formatPath, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) * { * System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalPath); * * int towidth = formatWidth; * int toheight = formatHeight; * * int x = 0; * int y = 0; * int ow = originalImage.Width; * int oh = originalImage.Height; * * if (cut) * { * //指定高宽裁减(不变形) * if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) * { * oh = originalImage.Height; * ow = originalImage.Height * towidth / toheight; * y = 0; * x = (originalImage.Width - ow) / 2; * } * else * { * ow = originalImage.Width; * oh = originalImage.Width * formatHeight / towidth; * x = 0; * y = (originalImage.Height - oh) / 2; * } * } * else * { * //默认指定高度宽度,失真缩放 * * //指定宽度缩放 * if (formatHeight <= 0) * { * formatWidth = originalImage.Width * formatHeight / originalImage.Height; * } * * //指定高度缩放 * else if (formatWidth <= 0) * { * formatHeight = originalImage.Height * formatWidth / originalImage.Width; * } * } * * //新建一个bmp图片 * System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); * //新建一个画板 * System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); * //设置高质量插值法 * graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; * //设置高质量,低速度呈现平滑程度 * graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; * //清空画布并以透明背景色填充 * graphics.Clear(System.Drawing.Color.Transparent); * //在指定位置并且按指定大小绘制原图片的指定部分 * graphics.DrawImage( * originalImage, * new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), * new System.Drawing.Rectangle(x, y, ow, oh), * System.Drawing.GraphicsUnit.Pixel * ); * originalImage.Dispose(); * * System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(picFormat); * * //以指定格式保存图片 * try * { * image.Save(formatPath, imageFormat); * } * catch (Exception ex) * { * throw ex; * } * finally * { * //originalImage.Dispose(); * image.Dispose(); * graphics.Dispose(); * } * } * */ /// <summary> /// 格式化图片 /// </summary> /// <param name="originalImagePath">源图物理路径</param> /// <param name="formatImagePath">新图物理路径</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="picFormat">图片输出格式</param> public static void FormatPicture(string originalPath, string formatPath, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) { Byte[] fileBinary = File.ReadAllBytes(originalPath); Byte[] formatBinary = FormatPicture(fileBinary, formatWidth, formatHeight, cut, picFormat); if (formatBinary != null) { FileConvert.SaveFile(formatPath, formatBinary); } }