how to get co-ordinates to write on image

V

Vikas Kumar

I am writing Hello on the image i am able to write it sucessfully now I want
to

write it at bottom right corner

How can I calculate the co-ordinates(suppose my image is 600*450)



string str="Hello";

Bitmap tempImage=new
System.Drawing.Bitmap(Server.MapPath("/Photos/21/Normal/1.jpg"));

Graphics graphicImage=System.Drawing.Graphics.FromImage(tempImage);

graphicImage.DrawString(str,new Font("Arial", 10,FontStyle.Regular),new
SolidBrush(Color.Black),new Point(0,0));

Response.ContentType="image/jpeg";

tempImage.Save("D:/test.jpg",ImageFormat.Jpeg);

graphicImage.Dispose();

tempImage.Dispose();
 
J

Jeffrey Tan[MSFT]

Hi Vikas,

Thanks for your post!

The Point you passed to DrawString method specifies which point to draw the
string. To draw in the bottom right corner of the image, you may pass the
bottom right point coordinate as the point parameter, and then pass an
extra StringFormat parameter to DrawString method. You should set
StringFormat.Alignment and LineAlignment properties to StringAlignment.Far
so that the string drawing start with a different alignment. Sample code
snippet is listed below:

Bitmap tempImage=new
System.Drawing.Bitmap(600, 450);
Graphics graphicImage=System.Drawing.Graphics.FromImage(tempImage);
string str="Hello";

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

graphicImage.DrawString(str,new Font("Arial", 10,FontStyle.Regular),new
SolidBrush(Color.Black),new Point(600,450), sf);

tempImage.Save(@"C:\test.jpg");
graphicImage.Dispose();
tempImage.Dispose();

Note:
1. in the code, I create an empty memory bitmap for drawing, and then save
it to disk. You can open this bitmap in disk to see the final effect.
2. the default StringFormat for DrawString method is StringAlignment.Near
which uses the (600, 450) point as the top-left drawing point, so draws the
string out of your image rectangle. By changing the alignment to
StringAlignment.Far, it will use (600, 450) point as the bottom-right
drawing point

You may modify the code snippet a little to use it in Asp.net HttpResponse
class.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

You are welcome

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top