水印格式以相反的方式出现

Watermark format is appearing in reverse way

根据代码,我得到的水印与输出完全相反。 以这种方式工作,如 pic.But 所示,我曾尝试像其他三角形一样反向获得它 帮帮我..我试了很多..

 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);

我仔细研究了一下,发现它不像加 90 度那么简单。

你通过旋转图形在偏移处绘制字符串让你自己变得更难,这意味着你仍在围绕原点旋转,但将字符串绘制得更远。

最终结果是,如果您旋转字符串,它就会消失(它已旋转到位图的边界之外)。

您需要做的是围绕弦的中心旋转。为此,在 before 旋转之前应用 Translate 变换,然后在 (0,0)

点绘制
        g.TranslateTransform(newBitmap.Width / 2, newBitmap.Height / 2);
        g.RotateTransform((float)-angle);
        g.DrawString(waterMarkText, font, new SolidBrush(color),
                     new Point(0, 0),
                     stringFormat);

您不再需要 halfHypotenuse 变量或代码。

现在当您编辑角度时,您应该可以清楚地看到效果。

要从左上角->右下角到左下角->右上角,使角度为负。

要求水印应该是这样的..知道了..谢谢朋友..

        Bitmap bmPhoto = new Bitmap(Width, Height);
        bmPhoto.SetResolution(Resolution, Resolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);
        Matrix matrix = new Matrix();

        String watermarkText = "For office use only";
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
        matrix.Translate(bmPhoto.Width / 2, bmPhoto.Height / 2);
        matrix.Rotate(-40.0f);// as per requirement we can give angle
        grPhoto.Transform = matrix;
        Font font = new Font("Verdana", 36, FontStyle.Bold, GraphicsUnit.Pixel);
        Color color = Color.FromArgb(100, 0, 0, 0); //Adds a transparent watermark with an 100 alpha value.
        SolidBrush sbrush = new SolidBrush(color);

        grPhoto.DrawString(watermarkText, font, new SolidBrush(color),0, 0,stringFormat);