Search code examples
c#asp.netgraphicssystem.drawing

how to draw string to an image from left bottom corner to right upper corner


Here is my code:

 Graphics g = Graphics.FromImage(newImage);

  var myBrush = new SolidBrush(Color.FromArgb(32, 205, 205, 205));
   g.DrawString(label, new Font("verdana", 10, GraphicsUnit.Pixel), myBrush, 10,10);

It doesn't work very well about put the string on the image. This code can write very well but I couldn't put the string from left buttom corner to right upper corner. Do you have any suggestion?


Solution

  • g.RotateTransform(45f);
    

    and only after that

    g.DrawString(label, new Font("verdana", 10, GraphicsUnit.Pixel), myBrush, 10,10);
    

    But that would only rotate by 45 degrees. Depending on size of your button and length of your text which can be calculated using MeasureString method, you will need to adjust the angle and position of the text. I hope you like geometry.

    http://msdn.microsoft.com/en-us/library/system.drawing.graphics.rotatetransform(v=vs.100).aspx

    In order to move the position where the string is drawn, you'll need to use the TranslateTransform(dx,dy) method of your graphics object.

    http://msdn.microsoft.com/en-us/library/6a1d65f4(v=vs.100).aspx

    So to draw string in center, you'd call translate then rotate then draw. If that doesn't work well try changing the order of rotate and translate.