首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#中的图像上的WaterMarking?

C#中的图像上的WaterMarking?
EN

Stack Overflow用户
提问于 2012-10-04 18:49:58
回答 3查看 4.2K关注 0票数 1

我想把水印放在图像的中心,diagonally.My代码做得很好,但是水印并不是出现在每个图像的中心,我认为水印文本的定位有问题,我硬编码values.How,我可以写出水印文本定位的通用公式吗?

代码语言:javascript
复制
    private MemoryStream ApplyWaterMark(MemoryStream stream)
        {

            System.Drawing.Image Im = System.Drawing.Image.FromStream(stream);                // 
            Graphics g = Graphics.FromImage(Im);

            // Create a solid brush to write the watermark text on the image
            Brush myBrush = new SolidBrush(System.Drawing.Color.FromArgb(25,
            System.Drawing.Color.LightSteelBlue));

            var f = new System.Drawing.Font(FontFamily.GenericSerif, 30);
            // Calculate the size of the text
            SizeF sz = g.MeasureString("TEST WATER MARK", f);

            int X, Y;
            X = ((int)(Im.Width - sz.Width) / 2)-1162;
            Y = ((int)(Im.Height + sz.Height) / 2)-127;



            g.RotateTransform(-45f);
            g.DrawString("TEST WATER MARK", f, myBrush,new System.Drawing.Point(X, Y));
            g.RotateTransform(45f);

            Im.Save(OutPutStream, ImageFormat.Png);//save image with dynamic watermark

            return OutPutStream;
        }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-16 01:32:00

您可以通过这种方式对角线设置水印

代码语言:javascript
复制
     Bitmap newBitmap = new Bitmap(bitmap);
     Graphics g = Graphics.FromImage(newBitmap);

     // Trigonometry: Tangent = Opposite / Adjacent
     double tangent = (double)newBitmap.Height / 
                      (double)newBitmap.Width;

     // convert arctangent to degrees
     double angle = Math.Atan(tangent) * (180/Math.PI);

     // a^2 = b^2 + c^2 ; a = sqrt(b^2 + c^2)
     double halfHypotenuse =(Math.Sqrt((newBitmap.Height 
                            * newBitmap.Height) +
                            (newBitmap.Width * 
                            newBitmap.Width))) / 2;

     // Horizontally and vertically aligned the string
     // This makes the placement Point the physical 
     // center of the string instead of top-left.
     StringFormat stringFormat = new StringFormat();
     stringFormat.Alignment = StringAlignment.Center;
     stringFormat.LineAlignment=StringAlignment.Center;

     g.RotateTransform((float)angle);            
     g.DrawString(waterMarkText, font, new SolidBrush(color),
                  new Point((int)halfHypotenuse, 0), 
                  stringFormat);
票数 6
EN

Stack Overflow用户

发布于 2012-10-04 19:46:04

尝试以这种方式计算图像中心的坐标(X,Y):

代码语言:javascript
复制
int X, Y;
X = (int)(Im.Width / 2 - sz.Width / 2);
Y = (int)(Im.Height / 2 - sz.Height / 2);
票数 0
EN

Stack Overflow用户

发布于 2014-04-24 18:28:16

底部有多种颜色的水印

代码语言:javascript
复制
public void AddWatermark(string watermarkText, string image, string TColor) 
{       

    System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Proot + image);
    Font font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);

    RectangleF rectf = new RectangleF(70, 90, 90, 50);

    Color color = Color.FromArgb(255, 255, 0, 0);
    try
    {
        if (TColor.ToUpper() == "RED")
        {
            color = Color.FromArgb(255, 255, 0, 0);
        }
        else if (TColor.ToUpper() == "WHITE")
        {
            color = Color.FromArgb(255, 255, 255, 255);
        }
        else if (TColor.ToUpper() == "BLACK")
        {
            color = Color.FromArgb(155, 0, 0, 0);
        }
        else if (TColor.ToUpper() == "GREEN")
        {
            color = Color.FromArgb(255, 0, 255, 0);
        }
    }
    catch { }


    //     WHITE (255, 255, 255, 255)
    //     RED   (255, 255, 000, 000)
    //     GREEN (255, 000, 255, 000)
    //     BLUE  (255, 000, 000, 255) 
    //     BLACK (150, 000, 000, 000)
    //     PURPLE(255, 125, 000, 255)
    //     GREY  (255, 128, 128, 128) 
    //     YELLOW(255, 255, 255, 000) 
    //     ORANGE(255, 255, 125, 000)

 //   Point atpoint = new Point(bitmap.Width / 2, bitmap.Height / 2);
    Point atpoint = new Point(bitmap.Width / 2, bitmap.Height - 10);

    SolidBrush brush = new SolidBrush(color);
    Graphics graphics = Graphics.FromImage(bitmap);

    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;

    graphics.DrawString(watermarkText, font, brush, atpoint, sf);
    graphics.Dispose();
    MemoryStream m = new MemoryStream();
    bitmap.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);

 //   Response.WriteFile("images/DefaultLogo.png", true);
    m.WriteTo(Response.OutputStream);

    m.Dispose();
    base.Dispose();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12725643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档