图片上传 自动生成大图小图 图片大小计算

        创建大图小图

void CreatBSimg(string extension,string path,string filename,string imgs)
        {

            #region 得到图片尺寸
            int imgWidth = 0;
            int imgHeight = 0;
            string[] imgExtension = new string[] { ".jpg", ".gif", ".png", ".bmp", ".jpeg" };
            foreach (string item in imgExtension)
            {
                if (item == extension)
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(imgs);
                    imgWidth = img.Width;
                    imgHeight = img.Height;


                    #region 新增 用于同时保存大图,和缩略图

                    //设置指定大小的上传
                    UploadImageFixedSize(img, path + "\\T" + filename, 100, 100);
                    //设置指定大小的上传
                    UploadImageFixedSize(img, path + "\\B" + filename, 800, 600);
                    #endregion


                    img.Dispose();
                }
            }
            #endregion
        }

保存图片
        public void UploadImageFixedSize(System.Drawing.Image image, string savePath, int width, int height)
        {
            Graphics g1 = null;
            Bitmap tImage = null;
            try
            {

                int oWidth = image.Width;//原图宽度
                int oHeight = image.Height;//原图高度
                int tWidth = width;//设置缩略图初始宽度
                int tHeight = height;//设置缩略图初始高度
                int outwidth = 0;
                int outheight = 0;
                ChangeImageSize(oWidth, oHeight, tWidth, tHeight, ref outwidth, ref outheight);

                tImage = new Bitmap(outwidth, outheight);
                g1 = Graphics.FromImage(tImage);
                g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
                g1.Clear(Color.Transparent);//清空画布并以透明背景色填充
                g1.DrawImage(image, new Rectangle(0, 0, outwidth, outheight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);

                //以JPG格式保存图片
                tImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Png);

            }
            catch (Exception ex)
            {
              
            }
            finally
            {
                //释放资源
                //image.Dispose();
                g1.Dispose();
                tImage.Dispose();
            }
        }

计算图片大小
        public void ChangeImageSize(float imgWidth, float imgHeight, int width, int height, ref int outwidth, ref int outheight)
        {
            try
            {
                if (imgHeight > height || imgWidth > width)
                {
                    if (imgHeight / height > imgWidth / width)
                    {
                        imgWidth = height / imgHeight * imgWidth;
                        imgHeight = height;
                    }
                    else
                    {
                        imgHeight = width / imgWidth * imgHeight;
                        imgWidth = width;
                    }
                }
                outwidth = Convert.ToInt32(imgWidth);
                outheight = Convert.ToInt32(imgHeight);
            }
            catch (Exception ce)
            {
            }
        }

转载于:https://www.cnblogs.com/wanheng/archive/2012/01/11/2319214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值