コード例 #1
0
        //上传主要部分。
        public void Open()
        {
            HttpPostedFile hpFile = _FormFile.PostedFile;

            if (hpFile == null || hpFile.FileName.Trim() == "")
            {
                _Error = 1;
                return;
            }

            string Ext = GetExt(hpFile.FileName);

            if (!IsUpload(Ext))
            {
                _Error = 2;
                return;
            }

            int iLen = hpFile.ContentLength;

            if (iLen > _MaxSize)
            {
                _Error = 3;
                return;
            }

            try
            {
                //判断文件夹是否存在.不存在创建!
                if (!Directory.Exists(_SavePath))
                {
                    Directory.CreateDirectory(_SavePath);
                }
                byte[] bData = new byte[iLen];
                hpFile.InputStream.Read(bData, 0, iLen);
                string FName;
                FName = FileName(Ext);
                string TempFile = "";
                if (_IsDraw)
                {
                    TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
                }
                else
                {
                    TempFile = FName;
                }
                FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
                newFile.Write(bData, 0, bData.Length);
                newFile.Flush();
                int _FileSizeTemp = hpFile.ContentLength;

                if (_IsDraw)
                {
                    if (_DrawStyle == 0)
                    {
                        System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
                        Graphics             g    = Graphics.FromImage(Img1);
                        g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
                        Font   f       = new Font(_Font, _FontSize);
                        Brush  b       = new SolidBrush(Color.Red);
                        string addtext = _AddText;
                        g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
                        g.Dispose();
                        Img1.Save(_SavePath + FName);
                        Img1.Dispose();
                    }
                    else
                    {
                        System.Drawing.Image image     = System.Drawing.Image.FromStream(newFile);
                        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(_CopyIamgePath));
                        Graphics             g         = Graphics.FromImage(image);
                        g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                        g.Dispose();
                        image.Save(_SavePath + FName);
                        image.Dispose();
                    }
                }

                try
                {
                    //获取图片的高度和宽度
                    System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
                    _Width  = Img.Width;
                    _Height = Img.Height;
                    //生成缩略图部分
                    if (_IsCreateImg)
                    {
                        //如果上传文件小于15k,则不生成缩略图。
                        if (iLen > 15360)
                        {
                            System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
                            newImg.Save(_SavePath + FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString());
                            newImg.Dispose();
                            _Iss = true;
                        }
                    }
                    if (_IsDraw)
                    {
                        if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
                        {
                            newFile.Dispose();
                            File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
                        }
                    }
                }
                catch { }
                newFile.Close();
                newFile.Dispose();
                _OutFileName = FName;
                _FileSize    = _FileSizeTemp;
                _Error       = 0;
                return;
            }
            catch
            {
                _Error = 4;
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// 上传图片(功能丰富)
        /// </summary>
        public void Upload()
        {
            HttpPostedFile hpFile = _FormFile.PostedFile;

            if (hpFile == null || hpFile.FileName.Trim() == "")
            {
                _Error = 1;
                return;
            }
            string Ext = GetExt(hpFile.FileName);

            if (!CheckImageType(Ext))
            {
                _Error = 2;
                return;
            }
            int iLen = hpFile.ContentLength;

            if (iLen > _MaxSize)
            {
                _Error = 3;
                return;
            }
            try
            {
                if (!Directory.Exists(_SavePath))
                {
                    Directory.CreateDirectory(_SavePath);
                }
                byte[] bData = new byte[iLen];
                hpFile.InputStream.Read(bData, 0, iLen);
                string FName;
                FName = FileName(Ext);
                string TempFile = "";
                if (_IsDraw)
                {
                    TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
                }
                else
                {
                    TempFile = FName;
                }
                FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
                newFile.Write(bData, 0, bData.Length);
                newFile.Flush();
                int _FileSizeTemp = hpFile.ContentLength;

                string ImageFilePath = _SavePath + FName;
                if (_IsDraw)
                {
                    if (_DrawStyle == 0)
                    {
                        System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
                        Graphics             g    = Graphics.FromImage(Img1);
                        g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
                        Font   f       = new Font(_Font, _FontSize);
                        Brush  b       = new SolidBrush(Color.Red);
                        string addtext = _AddText;
                        g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
                        g.Dispose();
                        Img1.Save(ImageFilePath);
                        Img1.Dispose();
                    }
                    else
                    {
                        System.Drawing.Image image     = System.Drawing.Image.FromStream(newFile);
                        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
                        Graphics             g         = Graphics.FromImage(image);
                        g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                        g.Dispose();
                        image.Save(ImageFilePath);
                        image.Dispose();
                    }
                }

                //获取图片的高度和宽度
                System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
                _Width  = Img.Width;
                _Height = Img.Height;

                //生成缩略图部分
                if (_IsCreateImg)
                {
                    #region 缩略图大小只设置了最大范围,并不是实际大小

                    float realbili = (float)_Width / (float)_Height;
                    float wishbili = (float)_sWidth / (float)_sHeight;

                    //实际图比缩略图最大尺寸更宽矮,以宽为准
                    if (realbili > wishbili)
                    {
                        _sHeight = (int)((float)_sWidth / realbili);
                    }
                    //实际图比缩略图最大尺寸更高长,以高为准
                    else
                    {
                        _sWidth = (int)((float)_sHeight * realbili);
                    }

                    #endregion

                    this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
                    string ImgFilePath = _SavePath + this.OutThumbFileName;

                    System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
                    newImg.Save(ImgFilePath);
                    newImg.Dispose();
                    _Iss = true;
                }

                if (_IsDraw)
                {
                    if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
                    {
                        newFile.Dispose();
                        File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
                    }
                }
                newFile.Close();
                newFile.Dispose();
                _OutFileName = FName;
                _FileSize    = _FileSizeTemp;
                _Error       = 0;
                return;
            }
            catch (Exception ex)
            {
                _Error = 4;
                return;
            }
        }
コード例 #3
0
        public void Upload()
        {
            HttpPostedFile hpFile = _FormFile;

            if (hpFile == null || hpFile.FileName.Trim() == "")
            {
                _Error = 1;
                return;
            }
            string Ext = GetExt(hpFile.FileName);

            if (!IsUpload(Ext))
            {
                _Error = 2;
                return;
            }
            int iLen = hpFile.ContentLength;

            if (iLen > _MaxSize)
            {
                _Error = 3;
                return;
            }
            try
            {
                if (!Directory.Exists(_SavePath))
                {
                    Directory.CreateDirectory(_SavePath);
                }
                byte[] bData = new byte[iLen];
                hpFile.InputStream.Read(bData, 0, iLen);
                string FName;
                FName = FileName(Ext);
                string TempFile = "";
                if (_IsDraw)
                {
                    TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
                }
                else
                {
                    TempFile = FName;
                }
                FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
                newFile.Write(bData, 0, bData.Length);
                newFile.Flush();
                int _FileSizeTemp = hpFile.ContentLength;

                string ImageFilePath = _SavePath + FName;
                if (_IsDraw)
                {
                    if (_DrawStyle == 0)
                    {
                        System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
                        Graphics             g    = Graphics.FromImage(Img1);
                        g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
                        Font   f       = new Font(_Font, _FontSize);
                        Brush  b       = new SolidBrush(Color.Red);
                        string addtext = _AddText;
                        g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
                        g.Dispose();
                        Img1.Save(ImageFilePath);
                        Img1.Dispose();
                    }
                    else
                    {
                        System.Drawing.Image image     = System.Drawing.Image.FromStream(newFile);
                        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
                        Graphics             g         = Graphics.FromImage(image);
                        g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                        g.Dispose();
                        image.Save(ImageFilePath);
                        image.Dispose();
                    }
                }

                //获取图片的高度和宽度
                System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
                _Width  = Img.Width;
                _Height = Img.Height;

                //生成小缩略图部分
                if (_IsCreateThumbImg)
                {
                    #region 缩略图大小只设置了最大范围,并不是实际大小

                    int x = 0;
                    int y = 0;

                    if (_Width <= _tWidth && _Height <= _tHeight)
                    {
                        _tWidth  = _Width;
                        _tHeight = _Height;
                    }
                    else
                    {
                        float realbili = (float)_Width / (float)_Height;
                        float wishbili = (float)_tWidth / (float)_tHeight;
                        switch (ThumbStyle)
                        {
                        case 1:
                            if (_Width <= _tWidth)
                            {
                                _tWidth  = _Width;
                                _tHeight = _Height;
                            }
                            else
                            {
                                _tHeight = (int)((float)_tWidth / realbili);
                            }
                            break;

                        case 2:
                            if (_Height <= _tHeight)
                            {
                                _tWidth  = _Width;
                                _tHeight = _Height;
                            }
                            else
                            {
                                _tWidth = (int)((float)_tHeight * realbili);
                            }
                            break;

                        case 3:
                            //实际图比缩略图最小尺寸更宽矮,以宽为准
                            if (realbili > wishbili)
                            {
                                int temp_width = _Width;
                                _Width = _Height * _tWidth / _tHeight;
                                y      = 0;
                                x      = (temp_width - _Width) / 2;
                            }
                            //实际图比缩略图最小尺寸更高长,以高为准
                            else
                            {
                                int temp_Height = _Height;
                                _Height = _Width * _tHeight / _tWidth;
                                x       = 0;
                                y       = (temp_Height - _Height) / 2;
                            }
                            break;

                        default:
                            //实际图比缩略图最大尺寸更宽矮,以宽为准
                            if (realbili > wishbili)
                            {
                                _tHeight = (int)((float)_tWidth / realbili);
                            }
                            //实际图比缩略图最大尺寸更高长,以高为准
                            else
                            {
                                _tWidth = (int)((float)_tHeight * realbili);
                            }
                            break;
                        }
                    }

                    #endregion

                    this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_t." + FName.Split('.').GetValue(1).ToString();
                    string ImgFilePath = _SavePath + this.OutThumbFileName;

                    //System.Drawing.Image newImg = Img.GetThumbnailImage(_tWidth, _tHeight, null, System.IntPtr.Zero);
                    //newImg.Save(ImgFilePath);
                    //newImg.Dispose();

                    System.Drawing.Image tImg = System.Drawing.Image.FromStream(newFile);
                    //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
                    Bitmap bmp = new Bitmap(_tWidth, _tHeight);
                    //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
                    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                    //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
                    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    //下面这个也设成高质量
                    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    //下面这个设成High
                    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //把原始图像绘制成上面所设置宽高的缩小图
                    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, _tWidth, _tHeight);
                    gr.DrawImage(tImg, rectDestination, x, y, _Width, _Height, GraphicsUnit.Pixel);
                    //保存图像
                    bmp.Save(ImgFilePath);
                    //释放资源
                    bmp.Dispose();
                    tImg.Dispose();

                    _IsThumbOK = true;
                }

                _Width  = Img.Width;
                _Height = Img.Height;

                //生成中缩略图部分
                if (_IsCreateMidImg)
                {
                    #region 缩略图大小只设置了最大范围,并不是实际大小

                    int x = 0;
                    int y = 0;

                    if (_Width <= _mWidth && _Height <= _mHeight)
                    {
                        _mWidth  = _Width;
                        _mHeight = _Height;
                    }
                    else
                    {
                        float realbili = (float)_Width / (float)_Height;
                        float wishbili = (float)_mWidth / (float)_mHeight;
                        switch (MidStyle)
                        {
                        case 1:
                            if (_Width <= _mWidth)
                            {
                                _mWidth  = _Width;
                                _mHeight = _Height;
                            }
                            else
                            {
                                _mHeight = (int)((float)_mWidth / realbili);
                            }
                            break;

                        case 2:
                            if (_Height <= _mHeight)
                            {
                                _mWidth  = _Width;
                                _mHeight = _Height;
                            }
                            else
                            {
                                _mWidth = (int)((float)_mHeight * realbili);
                            }
                            break;

                        case 3:
                            //实际图比缩略图最小尺寸更宽矮,以宽为准
                            if (realbili > wishbili)
                            {
                                int temp_width = _Width;
                                _Width = _Height * _mWidth / _mHeight;
                                y      = 0;
                                x      = (temp_width - _Width) / 2;
                            }
                            //实际图比缩略图最小尺寸更高长,以高为准
                            else
                            {
                                int temp_Height = _Height;
                                _Height = _Width * _mHeight / _mWidth;
                                x       = 0;
                                y       = (temp_Height - _Height) / 2;
                            }
                            break;

                        default:
                            //实际图比缩略图最大尺寸更宽矮,以宽为准
                            if (realbili > wishbili)
                            {
                                _mHeight = (int)((float)_mWidth / realbili);
                            }
                            //实际图比缩略图最大尺寸更高长,以高为准
                            else
                            {
                                _mWidth = (int)((float)_mHeight * realbili);
                            }
                            break;
                        }
                    }

                    #endregion

                    this.OutMidFileName = FName.Split('.').GetValue(0).ToString() + "_m." + FName.Split('.').GetValue(1).ToString();
                    string ImgFilePath = _SavePath + this.OutMidFileName;

                    //System.Drawing.Image newImg = Img.GetThumbnailImage(_mWidth, _mHeight, null, System.IntPtr.Zero);
                    //newImg.Save(ImgFilePath);
                    //newImg.Dispose();

                    System.Drawing.Image mImg = System.Drawing.Image.FromStream(newFile);
                    //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
                    Bitmap bmp = new Bitmap(_mWidth, _mHeight);
                    //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
                    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                    //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
                    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    //下面这个也设成高质量
                    gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    //下面这个设成High
                    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //把原始图像绘制成上面所设置宽高的缩小图
                    System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, _mWidth, _mHeight);
                    gr.DrawImage(mImg, rectDestination, x, y, _Width, _Height, GraphicsUnit.Pixel);
                    //保存图像
                    bmp.Save(ImgFilePath);
                    //释放资源
                    bmp.Dispose();
                    mImg.Dispose();

                    _IsMidOK = true;
                }

                if (_IsDraw)
                {
                    if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
                    {
                        newFile.Dispose();
                        File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
                    }
                }
                newFile.Close();
                newFile.Dispose();
                _OutFileName = FName;
                _FileSize    = _FileSizeTemp;
                _Error       = 0;
                return;
            }
            catch (Exception ex)
            {
                _Error = 4;
                return;
            }
        }