如何使用c#为图像添加水印

时间:2017-11-30 11:37:19

标签: c# asp.net

    string fname = "";
    string filename = ""; 
    if (context.Request.Files.Count > 0)
    {           
        HttpFileCollection files = context.Request.Files; 
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            string filess = files[i].FileName;


            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
            {
                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                fname = DateTime.Now.ToString("ddMMMyyyyhhmissmmm") + testfiles[testfiles.Length - 1];
                filename = filename + Path.GetFileName(fname);
            }
            else   
            {//some code here 
                fname = DateTime.Now.ToString("ddMMMyyyyhhmissmmm") + file.FileName;
                filename = filename + Path.GetFileName(fname);
            } 
            string replacestr = System.Text.RegularExpressions.Regex.Replace(filename, "[^a-zA-Z0-9_]+", " ");
            fname = replacestr;
            fname = replacestr;
            fname = fname.Replace(" ", "").Replace(",", "").Trim();
            fname = fname + "";
            fname = fname + ".png"; 
            filename = fname; 

fname = Path.Combine(context.Server.MapPath(“../ admin / img / product_images /”),fname);
                file.SaveAs(FNAME);

//这里有一些代码             }         }

1 个答案:

答案 0 :(得分:0)

//执行此工作水印enter image description here

            Bitmap bmp = new Bitmap(file.InputStream);
            Graphics graphicsobj = Graphics.FromImage(bmp);
            Brush brush = new SolidBrush(Color.FromArgb(80, 255, 255, 255));
            Point postionWaterMark = new Point((enter image description herebmp.Width / 6), (bmp.Height / 2));
            graphicsobj.DrawString("Daphnisys Technology", new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark);
            Image img = (Image)bmp;
            string filepath = HttpContext.Current.Server.MapPath("../admin/img/product_images/") + filename;
            //image.Save(filepath);
            img.Save(filepath, System.Drawing.Imaging.ImageFormat.Png);
            graphicsobj.Dispose();