Dzwebs.Net

撰写电脑技术杂文十余年

asp.net(C#)上传大图片并生成缩略图源代码

Admin | 2008-2-25 15:41:25 | 被阅次数 | 9606

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

//上传图片并生成缩略图
        public string Outputimg(HttpPostedFile fileUpload)
        {
            #region Outputimg
            Graphics g = null;
            System.Drawing.Image upimage = null;
            System.Drawing.Image thumimg = null;
            System.Drawing.Image simage = null;
            Bitmap outputfile = null;
            String filename = "";
            try
            {
                filename = String.Format("{0}{1}", Guid.NewGuid().ToString("N"), Path.GetExtension(fileUpload.FileName));
                string smallpath = "//111.111.1.111/uploadfile/smallimg/";
                string bigpath = "//111.111.1.111/uploadfile/bigimg/";
                int width, height, newwidth, newheight;
                System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                if (!Directory.Exists(smallpath))
                {
                    Directory.CreateDirectory(smallpath);
                }
                if (!Directory.Exists(bigpath))
                {
                    Directory.CreateDirectory(bigpath);
                }
                fileUpload.SaveAs("//111.111.1.111/uploadfile/bigimg/" + filename);

                Stream upimgfile = fileUpload.InputStream;
                string simagefile = "//111.111.1.111/uploadfile/bigimg/" + filename; //要加水印的文件
                simage = System.Drawing.Image.FromFile(simagefile);
                upimage = System.Drawing.Image.FromStream(upimgfile); //上传的图片
                width = upimage.Width;
                height = upimage.Height;
                //[color=#FF0000]宽、高[/color]
                thumimg = upimage.GetThumbnailImage(140, 120, callb, IntPtr.Zero);
                outputfile = new Bitmap(upimage);
                g = Graphics.FromImage(outputfile);
                g.DrawImage(simage, new Rectangle(upimage.Width - simage.Width, upimage.Height - simage.Height, upimage.Width, upimage.Height), 0, 0, upimage.Width, upimage.Height, GraphicsUnit.Pixel);


                thumimg.Save("//111.111.1.111/uploadfile/smallimg/" + filename);
                outputfile.Dispose();
            }
            catch
            {
                // throw ex;
            }
            finally
            {
                if (g != null)
                    g.Dispose();
                if (thumimg != null)
                    thumimg.Dispose();
                if (upimage != null)
                    upimage.Dispose();
                if (simage != null)
                    simage.Dispose();

            }
            return filename;
            #endregion
        }
        public bool ThumbnailCallback()
        {
            return false;
        }
//结束


该杂文来自: 网站开发杂文

上一篇:如何用.net打包自动安装数据库下篇

下一篇:宽带用户架设个人WEB服务器外网无法访问的解决方

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计