使用Graphics调整图片大小,用于替代GetThumbnailImage,解决GetThumbnailImage生成图片质量太差的问题。

 private void button1_Click(object sender, EventArgs e)
        {
            Image img_source = Image.FromFile("c:/920.jpg");
            int newWidth;
            int newHeight;
            //你自定义图的长宽如
            newWidth=600;
            newHeight = 400;
            Bitmap img_destion = new Bitmap(newWidth, newHeight);
            Graphics g = Graphics.FromImage(img_destion);
            g.DrawImage(img_source, 0, 0, newWidth, newHeight);
            img_destion.Save("c:/920new.jpg");
        }

        /// <summary>
        /// 图片大小调整
        /// </summary>
        /// <param name="img_souce_file">图片源文件</param>
        /// <param name="img_destion_file">图片目标文件</param>
        /// <param name="destion_size">调整图片目标大小</param>
        /// <param name="destion_size_iswidth">目标大小是宽度吗(true 调整目标大小为宽度 false调整目标大小为高度)</param>
        void image_resize(string img_souce_file,string img_destion_file,int destion_size,bool destion_size_iswidth)
        {
            Image img_source = Image.FromFile(img_souce_file);
            int newWidth;
            int newHeight;
            if (destion_size_iswidth)//按指定宽度进行调整
            {
                newWidth = destion_size;
                newHeight =(int)((float)img_source.Height/((float) img_source.Width/(float)newWidth));
            }
            else//按指定高度度进行调整
            {
                newHeight = destion_size;
                newWidth = (int)((float)img_source.Width / ((float)img_source.Height / (float)newHeight));
            }
           
            Bitmap img_destion = new Bitmap(newWidth, newHeight);
            Graphics g = Graphics.FromImage(img_destion);
            g.DrawImage(img_source, 0, 0, newWidth, newHeight);
            img_destion.Save(img_destion_file);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            image_resize("c:/920.jpg", "c:/920_new.jpg", Convert.ToInt16(textBox1.Text), checkBox1.Checked);
        }

上一篇:.NET(C#)使用GDI+绘图(Graphics)实现打印带有条码(BarcodeLib)的小票


下一篇:报表和仪表板生成器Stimulsoft v2021.3大版本更新!