其他分享
首页 > 其他分享> > 使用Graphics调整图片大小,用于替代GetThumbnailImage,解决GetThumbnailImage生成图片质量太差的问题。

使用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);
        }

标签:GetThumbnailImage,img,source,int,destion,newWidth,Graphics,图片大小,newHeight
来源: https://blog.csdn.net/zybpub/article/details/119409119