C#实现无损压缩图片的示例详解

目录
  • 实践过程
    • 效果
    • 代码

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    FileSystemInfo[] fsi = null;
    string ImgPath = "";
    ArrayList al = new ArrayList();
    string ImgSavePath = "";
    string strSourcePath = "";
    Thread td;
    Image ig = null;
    string strSavePath = "";

    private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
    }

    /// <summary>
    /// 无损图片缩放
    /// </summary>
    /// <param name="sFile">图片的原始路径</param>
    /// <param name="dFile">缩放后图片的保存路径</param>
    /// <param name="dHeight">缩放后图片的高度</param>
    /// <param name="dWidth">缩放后图片的宽度</param>
    /// <returns></returns>
    public static bool GetPicThumbnail(string sFile, string dFile, int dHeight, int dWidth)
    {
        Image iSource = Image.FromFile(sFile);
        ImageFormat tFormat = iSource.RawFormat;
        int sW = 0, sH = 0;
        // 按比例缩放
        Size tem_size = new Size(iSource.Width, iSource.Height);
        if (tem_size.Height > dHeight || tem_size.Width > dWidth)
        {
            if ((tem_size.Width * dHeight) > (tem_size.Height * 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 = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = 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] = 100;
        EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
        eP.Param[0] = eParam;
        try
        {
            //获得包含有关内置图像编码解码器的信息的ImageCodecInfo对象。
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICIinfo = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICIinfo = arrayICI[x]; //设置JPEG编码
                    break;
                }
            }

            if (jpegICIinfo != null)
            {
                oB.Save(dFile, jpegICIinfo, eP);
            }
            else
            {
                oB.Save(dFile, tFormat);
            }

            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            iSource.Dispose();
            oB.Dispose();
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            fsi = null;
            al.Clear();
            txtPicPath.Text = folderBrowserDialog1.SelectedPath;
            ImgPath = txtPicPath.Text.Trim();
            DirectoryInfo di = new DirectoryInfo(txtPicPath.Text);
            fsi = di.GetFileSystemInfos();
            for (int i = 0; i < fsi.Length; i++)
            {
                string ofile = fsi[i].ToString();
                string fileType = ofile.Substring(ofile.LastIndexOf(".") + 1, ofile.Length - ofile.LastIndexOf(".") - 1);
                fileType = fileType.ToLower();
                if (fileType == "jpeg" || fileType == "jpg" || fileType == "bmp" || fileType == "png")
                {
                    al.Add(ofile);
                }
            }

            lblPicNum.Text = al.Count.ToString();
        }
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        if (folderBrowserDialog2.ShowDialog() == DialogResult.OK)
        {
            txtSavePath.Text = folderBrowserDialog2.SelectedPath;
            ImgSavePath = txtSavePath.Text.Trim();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void rbPercent_Enter(object sender, EventArgs e)
    {
        groupBox1.Focus();
    }

    private void rbResolving_Enter(object sender, EventArgs e)
    {
        groupBox1.Focus();
    }

    private void CompleteIMG()
    {
        progressBar1.Maximum = al.Count;
        progressBar1.Minimum = 1;
        if (ImgPath.Length == 3)
            strSourcePath = ImgPath;
        else
            strSourcePath = ImgPath + "\\";

        if (ImgSavePath.Length == 3)
            strSavePath = ImgSavePath;
        else
            strSavePath = ImgSavePath + "\\";
        for (int i = 0; i < al.Count; i++)
        {
            ig = Image.FromFile(strSourcePath + al[i].ToString());
            if (rbPercent.Checked)
            {
                GetPicThumbnail(strSourcePath + al[i].ToString(), strSavePath + al[i].ToString(), Convert.ToInt32(ig.Width * (numericUpDown1.Value / 100)),
                    Convert.ToInt32(ig.Height * (numericUpDown1.Value / 100)));
            }

            ig.Dispose();
            progressBar1.Value = i + 1;
            lblComplete.Text = Convert.ToString(i + 1);
        }

        if (lblComplete.Text == al.Count.ToString())
        {
            MessageBox.Show("压缩成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            progressBar1.Value = 1;
            pictureBox1.Enabled = true;
            pictureBox2.Enabled = true;
            rbPercent.Enabled = true;
            lblPicNum.Text = "0";
            lblComplete.Text = "0";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (txtPicPath.Text.Trim() != "" && txtSavePath.Text.Trim() != "" && lblPicNum.Text != "0")
        {
            pictureBox1.Enabled = false;
            pictureBox2.Enabled = false;
            rbPercent.Enabled = false;
            td = new Thread(new ThreadStart(this.CompleteIMG));
            td.Start();
        }
        else
        {
            if (txtPicPath.Text.Trim() == "" && txtSavePath.Text.Trim() == "")
            {
                MessageBox.Show("警告:请选择待处理的图片目录及保存位置", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (txtPicPath.Text.Trim() == "")
                    MessageBox.Show("警告:请选择待处理的图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (txtSavePath.Text.Trim() == "")
                    MessageBox.Show("警告:请选择保存路径", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (td != null)
        {
            td.Abort();
        }
    }
}
 partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.txtSavePath = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.txtPicPath = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.rbPercent = new System.Windows.Forms.RadioButton();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.label13 = new System.Windows.Forms.Label();
            this.lblComplete = new System.Windows.Forms.Label();
            this.lblPicNum = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.folderBrowserDialog2 = new System.Windows.Forms.FolderBrowserDialog();
            this.groupBox1.SuspendLayout();
            this.panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.panel2);
            this.groupBox1.Controls.Add(this.panel1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new System.Drawing.Point(3, 1);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(458, 81);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "常规";
            //
            // panel2
            //
            this.panel2.BackColor = System.Drawing.Color.White;
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel2.Controls.Add(this.pictureBox2);
            this.panel2.Controls.Add(this.txtSavePath);
            this.panel2.Location = new System.Drawing.Point(130, 53);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(322, 21);
            this.panel2.TabIndex = 5;
            //
            // pictureBox2
            //
            this.pictureBox2.Cursor = System.Windows.Forms.Cursors.PanSE;
            this.pictureBox2.Image = global::CompressImg.Properties.Resources.文件夹打开;
            this.pictureBox2.Location = new System.Drawing.Point(300, 0);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(19, 18);
            this.pictureBox2.TabIndex = 8;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
            //
            // txtSavePath
            //
            this.txtSavePath.BackColor = System.Drawing.Color.White;
            this.txtSavePath.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtSavePath.Location = new System.Drawing.Point(0, 1);
            this.txtSavePath.Name = "txtSavePath";
            this.txtSavePath.ReadOnly = true;
            this.txtSavePath.Size = new System.Drawing.Size(302, 14);
            this.txtSavePath.TabIndex = 1;
            //
            // panel1
            //
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.pictureBox1);
            this.panel1.Controls.Add(this.txtPicPath);
            this.panel1.Location = new System.Drawing.Point(130, 23);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(322, 21);
            this.panel1.TabIndex = 4;
            //
            // pictureBox1
            //
            this.pictureBox1.Cursor = System.Windows.Forms.Cursors.PanSE;
            this.pictureBox1.Image = global::CompressImg.Properties.Resources.文件夹打开;
            this.pictureBox1.Location = new System.Drawing.Point(300, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(19, 18);
            this.pictureBox1.TabIndex = 8;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            //
            // txtPicPath
            //
            this.txtPicPath.BackColor = System.Drawing.Color.White;
            this.txtPicPath.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtPicPath.Location = new System.Drawing.Point(0, 1);
            this.txtPicPath.Name = "txtPicPath";
            this.txtPicPath.ReadOnly = true;
            this.txtPicPath.Size = new System.Drawing.Size(302, 14);
            this.txtPicPath.TabIndex = 1;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 57);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(125, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "处理后的图片保存至:";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(8, 28);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(125, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "等待处理的图片位置:";
            //
            // groupBox2
            //
            this.groupBox2.Controls.Add(this.numericUpDown1);
            this.groupBox2.Controls.Add(this.label4);
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.rbPercent);
            this.groupBox2.Location = new System.Drawing.Point(3, 86);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(458, 47);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "改变大小";
            //
            // numericUpDown1
            //
            this.numericUpDown1.Location = new System.Drawing.Point(188, 17);
            this.numericUpDown1.Maximum = new decimal(new int[] {
            200,
            0,
            0,
            0});
            this.numericUpDown1.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.numericUpDown1.Name = "numericUpDown1";
            this.numericUpDown1.Size = new System.Drawing.Size(40, 21);
            this.numericUpDown1.TabIndex = 8;
            this.numericUpDown1.Value = new decimal(new int[] {
            50,
            0,
            0,
            0});
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(234, 22);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(11, 12);
            this.label4.TabIndex = 4;
            this.label4.Text = "%";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(92, 22);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(89, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "改成原图大小的";
            //
            // rbPercent
            //
            this.rbPercent.AutoSize = true;
            this.rbPercent.Checked = true;
            this.rbPercent.Location = new System.Drawing.Point(10, 20);
            this.rbPercent.Name = "rbPercent";
            this.rbPercent.Size = new System.Drawing.Size(71, 16);
            this.rbPercent.TabIndex = 0;
            this.rbPercent.TabStop = true;
            this.rbPercent.Text = "按百分比";
            this.rbPercent.UseVisualStyleBackColor = true;
            this.rbPercent.Enter += new System.EventHandler(this.rbPercent_Enter);
            //
            // groupBox3
            //
            this.groupBox3.Controls.Add(this.progressBar1);
            this.groupBox3.Controls.Add(this.label13);
            this.groupBox3.Controls.Add(this.lblComplete);
            this.groupBox3.Controls.Add(this.lblPicNum);
            this.groupBox3.Controls.Add(this.label8);
            this.groupBox3.Controls.Add(this.label7);
            this.groupBox3.Location = new System.Drawing.Point(3, 139);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(458, 85);
            this.groupBox3.TabIndex = 9;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "操作显示";
            //
            // progressBar1
            //
            this.progressBar1.Location = new System.Drawing.Point(107, 54);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(302, 18);
            this.progressBar1.Step = 1;
            this.progressBar1.TabIndex = 16;
            //
            // label13
            //
            this.label13.AutoSize = true;
            this.label13.Location = new System.Drawing.Point(42, 57);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(53, 12);
            this.label13.TabIndex = 15;
            this.label13.Text = "处理进度";
            //
            // lblComplete
            //
            this.lblComplete.AutoSize = true;
            this.lblComplete.Location = new System.Drawing.Point(339, 25);
            this.lblComplete.Name = "lblComplete";
            this.lblComplete.Size = new System.Drawing.Size(11, 12);
            this.lblComplete.TabIndex = 12;
            this.lblComplete.Text = "0";
            //
            // lblPicNum
            //
            this.lblPicNum.AutoSize = true;
            this.lblPicNum.Location = new System.Drawing.Point(149, 26);
            this.lblPicNum.Name = "lblPicNum";
            this.lblPicNum.Size = new System.Drawing.Size(11, 12);
            this.lblPicNum.TabIndex = 11;
            this.lblPicNum.Text = "0";
            //
            // label8
            //
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(255, 26);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(77, 12);
            this.label8.TabIndex = 10;
            this.label8.Text = "已经处理成功";
            //
            // label7
            //
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(42, 26);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(101, 12);
            this.label7.TabIndex = 9;
            this.label7.Text = "待处理的图片共有";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(305, 230);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 10;
            this.button1.Text = "开始压缩";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(386, 230);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 11;
            this.button2.Text = "取消";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(463, 261);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "无损压缩图片";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox txtPicPath;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.RadioButton rbPercent;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Label label13;
        private System.Windows.Forms.Label lblComplete;
        private System.Windows.Forms.Label lblPicNum;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.TextBox txtSavePath;
        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog2;
        private System.Windows.Forms.NumericUpDown numericUpDown1;
    }

到此这篇关于C#实现无损压缩图片的示例详解的文章就介绍到这了,更多相关C#无损压缩图片内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#实现无损压缩图片代码示例

    一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的.尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,影响用户体验,所以一般会将图片进行压缩. 代码实现: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using Sys

  • C#无损压缩图片

    话不多说,请看代码: /// <summary> /// 根据指定尺寸得到按比例缩放的尺寸,返回true表示以更改尺寸 /// </summary> /// <param name="picWidth">图片宽度</param> /// <param name="picHeight">图片高度</param> /// <param name="specifiedWidth&quo

  • C#无损高质量压缩图片代码

    本文实例为大家分享了C#无损高质量压缩图片的具体代码,供大家参考,具体内容如下 /// 无损压缩图片 /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="dHeight">高度</param> /// <param name="

  • C#无损高质量压缩图片实现代码

    最近,项目上涉及到了图像压缩,发现原有的图像压缩功能,虽然保证了图像的大小300K以内,但是压缩后的图像看的不在清晰,并且,限定了图片的Height或者是Width. 在CSDN上看到了一个压缩算法:C#无损高质量压缩图片代码 进过测试这个算法,发现,将原始图像的大小进行对半处理,然后迭代跳转压缩质量参数,可以得到不错的效果. 修改后的算法如下: /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFi

  • C#实现无损压缩图片的示例详解

    目录 实践过程 效果 代码 实践过程 效果 代码 public partial class Form1 : Form { public Form1() { InitializeComponent(); } FileSystemInfo[] fsi = null; string ImgPath = ""; ArrayList al = new ArrayList(); string ImgSavePath = ""; string strSourcePath = &q

  • iOS中常见的视图和图片处理示例详解

    前言 众所周知在开发中不可避免的会遇到一些图片和视图的处理,我这里总结的这些只是我遇到的一些,以供下次使用查看.下面话不多说了,来一起看看详细的介绍吧. 图片的旋转 是UIImage的扩展类,直接使用UIImage的对象调用即可 UIImage #import <QuartzCore/QuartzCore.h> #import <Accelerate/Accelerate.h> @implementation UIImage (ImageRotate) -(UIImage *)im

  • Java实现图片裁剪功能的示例详解

    目录 前言 Maven依赖 代码 验证一下 前言 本文提供将图片按照自定义尺寸进行裁剪的Java工具类,一如既往的实用主义. Maven依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency> <dependen

  • jmeter添加自定义扩展函数之图片base64编码示例详解

    打开eclipse,新建maven工程,在pom中引入jmeter核心jar包: <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core --> <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <v

  • Python实现为图片添加水印的示例详解

    目录 1.引言 2.filestools介绍 2.1 安装 2.2 filestools 功能介绍 2.3 watermarker模块介绍 2.4 代码实例 补充 1.引言 小屌丝:鱼哥,这个周末过得咋样 小鱼:酸爽~ ~ 小屌丝:额~~ 我能想到的,是这样吗? 小鱼:有多远你走多远. 小屌丝:唉,鱼哥,你别说,我觉得这个图片,跟你平时的表情挺贴切的. 小鱼:你想咋的!!!! 小屌丝:突然想到,能不能给你来一个专属的图片,例如追加水印啥的,让别人无图可盗!! 小鱼:嘿~ 你别说,还真的可以哈,

  • Java实现图片合成的示例详解

    目录 场景 环境 搭建 引入pom文件 定义核心接口ImageService 定义核心接口实现类ImageServiceImpl 测试ImageController 测试效果 总结 场景 前端有一个神器——canvas,这个画布标签可以处理各种图片的合成,可以精确到图片的具体坐标,加水印,去水印,简直不要太简单!那java后端可以处理吗?请大声的告诉他,能,必须能!今天小编告诉你一个神器——image-combiner,合成图片so easy! 环境 jdk1.8 spring boot 搭建

  • java zxing合成复杂二维码图片示例详解

    目录 说明: 整体思路: 图片合成四部曲 踩过的坑 说明: 最近接到需要将二维码合成复杂图片的需求,要求给二维码上下或者左侧添加相关文字描述,技术没有难点,整理本文主要记录思路和踩过的坑. 整体思路: 引入zxing成熟的二维码生成接口,生成标准二维码文件,通过java图形图像处理API为二维码添加相关文字描述,根据需要,可以为合成后的图片添加相关背景.示例如下图所示: 1.先拿点位图来说,生成二维码图片核心代码如下 /** * 定义二维码的参数 */ HashMap<EncodeHintTyp

  • Python计算图片数据集的均值方差示例详解

    目录 前言 Python批量reshape图片 参考 计算数据集均值和方差 前言 在做图像处理的时候,有时候需要得到整个数据集的均值方差数值,以下代码可以解决你的烦恼: (做这个之前一定保证所有的图片都是统一尺寸,不然算出来不对,我的代码里设计的是512*512,可以自己调整,同一尺寸的代码我也有: Python批量reshape图片 # -*- coding: utf-8 -*- """ Created on Thu Aug 23 16:06:35 2018 @author

  • Python Flask实现图片上传与下载的示例详解

    目录 1.效果预览 2.新增逻辑概览 3.tuchuang.py 逻辑介绍 3.1 图片上传 3.2 图片合法检查 3.3 图片下载 4.__init__.py 逻辑介绍 5.upload.html 介绍 5.1 upload Jinja 模板介绍 5.2 upload css 介绍(虚线框) 5.3 upload js 介绍(拖拽) 1.效果预览 我们基于 Flask 官方指导工程,增加一个图片拖拽上传功能,效果如下: 2.新增逻辑概览 我们在官方指导工程上进行增加代码,改动如下: 由于 fl

  • 不调用方法实现hutool导出excel图片示例详解

    目录 前言 骚操作 输出excel数据代码 导出类 输出图片 展示结果 吐槽 前言 最近在做excel导出文件,然后有一列是图片展示,然后我们图片搞了防盗链,如果直接点开链接,就是一个默认图片(无法展示),我就想着把图片嵌入excel中展示,由于我框架用的是hutool去导出,我点开里面各种类,结果都没有img的输入excel的方法,气死我了 骚操作 其实我也是一个cv工程师,百度找找有没有大佬已经实现这功能,然后就找到了,不是hutool里面的方法,是poi包下 输出excel数据代码 //写

随机推荐