C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Images »

Custom drawing in Images


Posted Date: 13 Apr 2009    Resource Type: Code Snippets    Category: Images
Author: shobhit jainMember Level: Silver    
Rating: 1 out of 5Points: 7



This Code May Be used to Edit An Existing Image and Save it as a New Image

Bitmap thumbImage = null;
try
{
thumbImage = new Bitmap(@"c:\Image.jpg");
Graphics finalImage = Graphics.FromImage(thumbImage);

//Custom drawing
if (sDraw != null && sDraw != "")
{
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
SizeF size = finalImage.MeasureString(sDraw, drawFont);

Matrix mx = new Matrix(1, 0, 0, 1, 0, 0);

mx.Rotate(315, MatrixOrder.Append);

mx.Translate(thumbImage.Width / 2, thumbImage.Height / 2, MatrixOrder.Append);

finalImage.Transform = mx;

SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
finalImage.DrawString(sDraw, drawFont, drawBrush, -size.Width / 2, -size.Height / 2, StringFormat.GenericTypographic);
}

finalImage.Dispose();
thumbImage.Save(@"c:\NewImage.jpg");
}
catch (Exception ex)
{
thumbImage = null;
}


Note : THe variable 'sDraw' contains the value to draw on the Image.



Responses

Author: nisar    18 Apr 2009Member Level: Gold   Points : 1
i want to add text on image, this code have this functionality, or of you know how to achieve this functionality then please guide me
thanks


Author: shobhit jain    20 Apr 2009Member Level: Silver   Points : 1
Yes this code can add the text on image. simply fill your text in sDraw variable. The text will be added as a watermark. For adding text at other directions change the matrix used.


Author: nisar    20 Apr 2009Member Level: Gold   Points : 1
Thanks for your reply, but i am unable to use matrix, mean i dnt know which class support matrix?


Author: shobhit jain    20 Apr 2009Member Level: Silver   Points : 0
namespace System.Drawing.Drawing2D contains Matrix class.


Author: nisar    20 Apr 2009Member Level: Gold   Points : 1
thanks for your quick reply, now this code works for me, but the problem is when i insert text, It inserted at the middle with rotated, but i want to insert this text on left upper corner with straight text, please guide me how can achieve this......


Author: shobhit jain    20 Apr 2009Member Level: Silver   Points : 1
remove the lines mx.Rotate(315, MatrixOrder.Append); and
mx.Translate(thumbImage.Width / 2, thumbImage.Height / 2, MatrixOrder.Append);

Add the line mx.Translate(0, 0, MatrixOrder.Append);


Author: nisar    20 Apr 2009Member Level: Gold   Points : 1
thanks shobhit jain, now its work perfectly according to my requirement, keep posting such codes....


Author: shobhit jain    20 Apr 2009Member Level: Silver   Points : 1
remove the lines mx.Rotate(315, MatrixOrder.Append); and
mx.Translate(thumbImage.Width / 2, thumbImage.Height / 2, MatrixOrder.Append);

Add the line mx.Translate(0, 0, MatrixOrder.Append);


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Image  .  Custom drawing  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Writing a gif to a browser request for an img tag
Previous Resource: Save image into Database and folder in C#
Return to Discussion Resource Index
Post New Resource
Category: Images


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use