Exemplo n.º 1
0
    //上传
    protected void BtnUpLoad_Click(object sender, EventArgs e)
    {
        if (FileUploadFiles.HasFile)
        {
            ArrayList arrylist = new ArrayList();
            arrylist.Add("jpg");
            arrylist.Add("jpeg");
            arrylist.Add("png");
            arrylist.Add("ico");
            arrylist.Add("gif");
            arrylist.Add("bmp");
            arrylist.Add("rar");
            arrylist.Add("txt");
            arrylist.Add("doc");
            arrylist.Add("txt");
            arrylist.Add("zip");
            arrylist.Add("swf");
            arrylist.Add("mp3");

            ArrayList alType = new ArrayList();
            alType.Add("image/pjpeg");
            alType.Add("image/gif");
            alType.Add("image/bmp");
            alType.Add("image/png");
            alType.Add("image/x-icon");
            alType.Add("image/jpeg");
            alType.Add("image/x-png");
            alType.Add("application/octet-stream");
            alType.Add("application/msword");
            alType.Add("text/plain");
            alType.Add("application/x-shockwave-flash");
            alType.Add("audio/mpeg");

            string   fileName    = FileUploadFiles.FileName;
            string[] houzhuiList = fileName.Split(new char[] { '.' });
            string   houzhui     = houzhuiList[houzhuiList.Length - 1].ToLower();

            if (!string.IsNullOrEmpty(fileSuffix))
            {
                List <string> suffix = new List <string>();
                suffix.AddRange(fileSuffix.Split(','));
                if (!suffix.Contains(houzhui))
                {
                    LbTishi.Text = "上传文件类型错误,请上传" + fileSuffix + "类型文件";
                    return;
                }
                else
                {
                    LbTishi.Text = "";
                }
            }

            if (arrylist.Contains(houzhui))
            {
                if (alType.Contains(FileUploadFiles.PostedFile.ContentType))
                {
                    if (FileUploadFiles.PostedFile.ContentLength / (1024 * 1024) >= 20)
                    {
                        LbTishi.Text = "上传文件过大";
                    }
                    else
                    {
                        string strRand = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString(); //日期
                        Random rand    = new Random();
                        strRand += rand.Next(10000000, 99999999);                                                                                                   //随机数

                        string ImgName = strRand + "." + houzhui;                                                                                                   //文件名称

                        if (directoryUrl == null || directoryUrl == string.Empty)
                        {
                            directoryUrl = "Userfiles/";
                        }

                        //保存到数据库的路径
                        string strSqlUrl = directoryUrl + ImgName;

                        //上传路径
                        string strImgUrl = Server.MapPath("~/" + directoryUrl) + ImgName;

                        //核查目录,如果不存在就创建该目录
                        if (!Directory.Exists(Server.MapPath("~/" + directoryUrl)))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/" + directoryUrl));
                        }

                        FileUploadFiles.SaveAs(strImgUrl);//上传

                        #region 水印
                        if (isWaterMark == true)
                        {
                            string          fileUrl = HttpContext.Current.Server.MapPath("~/App_Data/WebSet/WebSet.xml");
                            YK.Model.WebSet webset  = YK.Common.MyXmlSerializer <YK.Model.WebSet> .Get(fileUrl);

                            switch (webset.WaterMarkType)
                            {
                            case -1:
                                break;

                            case 0:
                                YK.Common.WaterMarkHelper.TxtWaterMark(strImgUrl);
                                break;

                            case 1:
                                YK.Common.WaterMarkHelper.PicWaterMark(strImgUrl);
                                break;
                            }
                        }
                        #endregion

                        TbFileUrl.Visible       = true;
                        ImageShow.Visible       = true;
                        FileUploadFiles.Visible = false;
                        BtnUpLoad.Visible       = false;
                        BtnDelete.Visible       = true;
                        //TbFileUrl.Enabled = false;

                        TbFileUrl.Text = strSqlUrl;
                        ImageShow.Src  = "../../" + strSqlUrl;
                        url            = strSqlUrl;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
        /// <summary>
        /// 给图片上水印
        /// </summary>
        /// <param name="path">加水印图片的物理路径</param>
        public static void PicWaterMark(string filePath)
        {
            string fileUrl = HttpContext.Current.Server.MapPath("~/App_Data/WebSet/WebSet.xml");

            YK.Model.WebSet webset = MyXmlSerializer <YK.Model.WebSet> .Get(fileUrl);

            string waterFile = HttpContext.Current.Server.MapPath("~/" + webset.WaterMarkPicUrl);

            string   ModifyImagePath = filePath; //修改的图像路径
            int      lucencyPercent  = 25;       //透明度
            Image    modifyImage     = null;
            Image    drawedImage     = null;
            Graphics g = null;

            //建立图形对象
            modifyImage = Image.FromFile(ModifyImagePath, true);
            drawedImage = Image.FromFile(waterFile, true);
            g           = Graphics.FromImage(modifyImage);
            //设置颜色矩阵
            float[][] matrixItems =
            {
                new float[] { 1, 0, 0,                            0, 0 },
                new float[] { 0, 1, 0,                            0, 0 },
                new float[] { 0, 0, 1,                            0, 0 },
                new float[] { 0, 0, 0, (float)lucencyPercent / 100f, 0 },
                new float[] { 0, 0, 0,                            0, 1 }
            };

            //获取要绘制图形坐标
            int x = 0;
            int y = 0;
            //水印图片的宽和高
            int width  = drawedImage.Width;
            int height = drawedImage.Height;



            switch (webset.WaterMarkHorizontal)
            {
            case "left":
                x = 0;
                break;

            case "center":
                x = (modifyImage.Width - width) / 2;
                break;

            case "right":
                x = modifyImage.Width - width;
                break;
            }

            switch (webset.WaterMarkVertical)
            {
            case "top":
                y = 0 + 10;     //向下移动10像素好看些
                break;

            case "middle":
                y = (modifyImage.Height - height) / 2;
                break;

            case "bottom":
                y = modifyImage.Height - height;
                break;
            }

            ColorMatrix     colorMatrix = new ColorMatrix(matrixItems);
            ImageAttributes imgAttr     = new ImageAttributes();

            imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            //绘制阴影图像
            g.DrawImage(drawedImage,
                        new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
                        0, 0, //水印图片的坐标值开始,如0,0就是从水印图片的0,0坐标开始画图
                        drawedImage.Width,
                        drawedImage.Height,
                        GraphicsUnit.Pixel,
                        imgAttr);
            //保存文件
            string[]    allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
            FileInfo    fi             = new FileInfo(ModifyImagePath);
            ImageFormat imageType      = ImageFormat.Gif;

            switch (fi.Extension.ToLower())
            {
            case ".jpg": imageType = ImageFormat.Jpeg; break;

            case ".gif": imageType = ImageFormat.Gif; break;

            case ".png": imageType = ImageFormat.Png; break;

            case ".bmp": imageType = ImageFormat.Bmp; break;

            case ".tif": imageType = ImageFormat.Tiff; break;

            case ".wmf": imageType = ImageFormat.Wmf; break;

            case ".ico": imageType = ImageFormat.Icon; break;

            default: break;
            }
            MemoryStream ms = new MemoryStream();

            modifyImage.Save(ms, imageType);
            byte[] imgData = ms.ToArray();
            drawedImage.Dispose();
            modifyImage.Dispose();
            g.Dispose();

            FileStream fs = null;

            File.Delete(ModifyImagePath);
            fs = new FileStream(ModifyImagePath, FileMode.Create, FileAccess.Write);
            if (fs != null)
            {
                fs.Write(imgData, 0, imgData.Length);
                fs.Close();
            }
        }