工具类代码:
using System; System.Collections.Generic; System.Drawing; System.Drawing.drawing2d; System.Drawing.Imaging; System.IO; System.Linq; System.Text; System.Threading.Tasks; namespace ZoomImage.Utils { /// <summary> /// 图片缩放 </summary> public class ZoomImageUtil { #region 图片缩放 <summary> 图片缩放 </summary> <param name="bArr">图片字节流</param> <param name="width">目标宽度,若为0,表示宽度按比例缩放<param name="height">目标长度,若为0,表示长度按比例缩放</param> static byte[] GetThumbnail(byte[] bArr,int width,1)">int height) { if (bArr == null) return null; MemoryStream ms = new MemoryStream(bArr); Bitmap bmp = (Bitmap)Image.FromStream(ms); ms.Close(); bmp = GetThumbnail(bmp,width,height); ImageCodecInfo imageCodecInfo = GetEncoder(ImageFormat.Jpeg); EncoderParameters encoderParameters = new EncoderParameters(1); EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,75L); encoderParameters.Param[0] = encoderParameter; ms = MemoryStream(); bmp.Save(ms,imageCodecInfo,encoderParameters); byte[] result = ms.ToArray(); ms.Close(); bmp.dispose(); return result; } #endregion <param name="bmp">图片private static Bitmap GetThumbnail(Bitmap bmp,1)">if (width == 0 && height == 0) { width = bmp.Width; height = bmp.Height; } else { ) { width = height * bmp.Width / bmp.Height; } if (height == ) { height = width * bmp.Height / bmp.Width; } } Image imgSource = bmp; Bitmap outBmp = Bitmap(width,height); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Transparent); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.Default; g.SmoothingMode = SmoothingMode.Default; g.InterpolationMode = InterpolationMode.Default; g.DrawImage(imgSource,new Rectangle(0,height + 1),1)">,imgSource.Width,imgSource.Height,GraphicsUnit.Pixel); g.dispose(); imgSource.dispose(); bmp.dispose(); outBmp; } #region 椭圆形缩放 椭圆形缩放 byte[] GetEllipseThumbnail( (Bitmap)Image.FromStream(ms); Bitmap newBmp = using (Graphics g = Graphics.FromImage(newBmp)) { using (TextureBrush br = TextureBrush(bmp)) { br.ScaleTransform(width / (float)bmp.Width,height / (float)bmp.Height); g.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.AntiAlias; g.FillEllipse(br,1)">new Rectangle(Point.Empty,1)"> Size(width,height))); } } MemoryStream newMs = MemoryStream(); newBmp.Save(newMs,System.Drawing.Imaging.ImageFormat.Png); newMs.ToArray(); bmp.dispose(); newBmp.dispose(); ms.Close(); newMs.dispose(); #region GetEncoder static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.Getimagedecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { codec; } } ; } #endregion } }
使用示例:
System.ComponentModel; System.Data; System.Threading; System.Threading.Tasks; System.Windows.Forms; ZoomImage.Utils; ZoomImage { partial Form1 : Form { public Form1() { InitializeComponent(); } void Form1_Load(object sender,EventArgs e) { openFileDialog1.Multiselect = true; } void txtWidth_KeyPress(if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = ; } } void txtHeight_KeyPress(void btnSelectimage_Click(tryif (txtWidth.Text == "" && txtHeight.Text == "") { MessageBox.Show("请输入宽度或高度!"); ; } if (openFileDialog1.ShowDialog() == DialogResult.OK) { Task.Factory.StartNew(() => { { string path = Path.GetDirectoryName(openFileDialog1.FileNames[0]) + \\NewImage\\; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (string file Directory.GetFiles(path)) { File.Delete(file); } int i = string fileName openFileDialog1.FileNames) { Bitmap bmp = ZoomImageUtil.GetThumbnail(new Bitmap(fileName),Convert.ToInt32(txtWidth.Text == "" ? 0" : txtWidth.Text),Convert.ToInt32(txtHeight.Text == : txtHeight.Text)); this.Invoke(new InvokeDelegate(() => { if (cbxExt.SelectedItem == ) { bmp.Save(path + Path.GetFileName(fileName)); } { bmp.Save(path + Path.GetFileNameWithoutExtension(fileName) + (string)cbxExt.SelectedItem); } })); { lblProgress.Text = string.Format(进度:{1}/{0}",openFileDialog1.FileNames.Length,++i); })); Thread.Sleep(); } MessageBox.Show(成功!); } catch (Exception ex) { MessageBox.Show(ex.Message); } }); } } (Exception ex) { MessageBox.Show(ex.Message); } } } 跨线程访问控件的委托 delegate void InvokeDelegate(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。