在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#图形和图像处理 → 一键格式化图片的小工具

一键格式化图片的小工具

C#图形和图像处理

下载此实例
  • 开发语言:C#
  • 实例大小:0.23M
  • 下载次数:6
  • 浏览次数:105
  • 发布时间:2021-07-19
  • 实例类别:C#图形和图像处理
  • 发 布 人:熊猫仙
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 格式化 工具 图片

实例介绍

【实例简介】

用户有上传本地图片的需求,但是服务器对图片的格式和尺寸有要求,用户冒然上传会不成功,做个小工具,注册后在图片上点击鼠标右键,在弹出的菜单里有格式转换的选项,一键转换为符合要求的图片格式,支持多选.

【实例截图】

from clipboard

【核心代码】

/// <summary>
        /// 转换
        /// </summary>
        private void PictureConvert()
        {
            //转换
            if (this.lvwPictureFile.Items.Count > 0)
            {
                bool _RunFlag = true;
                foreach (ListViewItem _lvi in this.lvwPictureFile.Items)
                {
                    //MessageBox.Show(Convert.ToString(_lvi.SubItems[0].Text));

                    if (_lvi.Text == "等待转换")
                    {
                        try
                        {
                            //定义输入文件
                            string LoadFile = _lvi.SubItems[1].Text;

                            //定义输出文件(分离全文件名)
                            string[] _Item = LoadFile.Split('\\');
                            string _FileItem = _Item[_Item.Length - 1];
                            string LoadPath = "";
                            for (int _i = 0; _i < _Item.Length - 1; _i )
                            {
                                if (LoadPath == "")
                                {
                                    LoadPath = _Item[_i];
                                }
                                else
                                {
                                    LoadPath = LoadPath "\\" _Item[_i];
                                }
                            }
                            //定义输出文件(分离文件名和扩展名)
                            string[] _SubItem = _FileItem.Split('.');
                            string FileItemMain = "";
                            for (int _i = 0; _i < _SubItem.Length - 1; _i )
                            {
                                if (FileItemMain == "")
                                {
                                    FileItemMain = _SubItem[_i];
                                }
                                else
                                {
                                    FileItemMain = FileItemMain "." _SubItem[_i];
                                }
                            }
                            //组合输出文件
                            string SaveFile = LoadPath "\\(反假)" FileItemMain ".jpg";

                            //装入源文件
                            Bitmap SrcImage = new Bitmap(LoadFile);
                            //循环降低照片分辨率从而找到适合的容量
                            double FileLength = 1024 * 1024 * 2;
                            for (float _i = 1f; _i > 0.1f; _i = _i - 0.1f)
                            {
                                //定义新的图片尺寸及图片对象
                                int DestWidth = (int)(SrcImage.Width * _i);
                                int DestHeight = (int)(SrcImage.Height * _i);
                                Bitmap DestImage = new Bitmap(DestWidth, DestHeight);
                                //进行图片转换
                                Graphics _Graphics = Graphics.FromImage(DestImage);
                                //将源图像画到画布上,注意最后一个参数GraphicsUnit.Pixel
                                _Graphics.DrawImage(SrcImage, new Rectangle(0, 0, DestWidth, DestHeight), new Rectangle(0, 0, SrcImage.Width, SrcImage.Height), GraphicsUnit.Pixel);
                                //设置压缩//设置压缩质量为level 75
                                EncoderParameters _EncoderParameters = new EncoderParameters();
                                _EncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
                                //获取指定mimeType的ImageCodecInfo
                                ImageCodecInfo _ImageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(ici => ici.MimeType == "image/jpeg");
                                //获取转换后的图片大小
                                MemoryStream _MemoryStream = new MemoryStream();
                                DestImage.Save(_MemoryStream, _ImageCodecInfo, _EncoderParameters);                            //判断图片大小
                                if (_MemoryStream.Length < FileLength)
                                {
                                    //DestImage.Save(SaveFile, ImageFormat.Jpeg);
                                    DestImage.Save(SaveFile, _ImageCodecInfo, _EncoderParameters);
                                    //MessageBox.Show("转换完毕!");
                                    _lvi.ImageIndex = 1;
                                    _lvi.Text = "转换完毕";

                                    break;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            //MessageBox.Show("软件不支持的图片格式!请联系客服人员");
                            _lvi.ImageIndex = 0;
                            _lvi.Text = "不支持的图片格式";
                            _RunFlag = false;
                        }
                    }
                }
                if (_RunFlag == false)
                {
                    MessageBox.Show("遇到不支持的图片格式!请联系客服人员", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

标签: 格式化 工具 图片

实例下载地址

一键格式化图片的小工具

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警