zoukankan      html  css  js  c++  java
  • .Net给图片加水印,并解决“无法从带有索引像素格式的图像创建Graphics对象”问题

     using (Image img = Image.FromFile(savePath))

    {
    //如果原图片是索引像素格式之列的,则需要转换
    if (img.PixelFormat!=null)
    {
    Bitmap bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmp))
    {
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

    g.DrawImage(img, 0, 0, bmp.Width, bmp.Height);
    Font f = new Font("微软雅黑", 32); 

    Brush b = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
    string addText = "仅供预览, 此件打印无效";
    StringFormat Format = new StringFormat();
    Format.Alignment = StringAlignment.Center; //居中
    Rectangle rec = new Rectangle(0, 0, img.Width, img.Height);

    Format.LineAlignment = StringAlignment.Center;
    g.DrawString(addText, f, b, rec, Format);
    g.Dispose();
    bmp.Save(savePath + "_");
    }
    }
    else
    {
    System.Drawing.Image image = System.Drawing.Image.FromFile(savePath);
    Graphics g = Graphics.FromImage(image);
    g.DrawImage(image, 0, 0, image.Width, image.Height);
    Font f = new Font("微软雅黑", 32);
    Brush b = new SolidBrush(Color.FromArgb(150, 255, 0, 0));
    string addText = "仅供预览, 此件打印无效";
    StringFormat Format = new StringFormat();
    Format.Alignment = StringAlignment.Center; //居中

    Format.LineAlignment = StringAlignment.Center; //居中

    Rectangle rec = new Rectangle(0, 0, image.Width, image.Height);

    g.DrawString(addText, f, b, rec, Format);

    g.Dispose();
    image.Save(savePath + "_");
    }
    }

    效果如下

  • 相关阅读:
    css 三种布局定位
    居中布局注意的要点
    CSS盒子3D模型
    SpringMVC中JSP取不到ModelAndView的数据原因
    如何让 height:100%; 起作用
    css形变
    消除html元素之间空白
    h5移动端网页头部标签模板
    大数据之Hadoop核心之HDFS
    大数据之Hadoop集群搭建
  • 原文地址:https://www.cnblogs.com/tianxiaotian/p/9777854.html
Copyright © 2011-2022 走看看