Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘text’

Convert Text in to Image using C#

Posted by Viral Sarvaiya on December 13, 2011


.net provide us very good functionality to Create image from text, here is function that return Bitmap and take string as a parameter.


private Bitmap CreateBitmapImage(string TextImage)
{
Bitmap objBmp = new Bitmap(1, 1);

int Width = 0;

int Height = 0;

// Create the Font object for the image text drawing.

Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

// Create a graphics object to measure the text's width and height.

Graphics objGraphics = Graphics.FromImage(objBmp);

// This is where the bitmap size is determined.

Width = (int)objGraphics.MeasureString(TextImage, objFont).Width;

Height = (int)objGraphics.MeasureString(TextImage, objFont).Height;

// Create the bmpImage again with the correct size for the text and font.

objBmp = new Bitmap(objBmp, new Size(Width, Height));

// Add the colors to the new bitmap.

objGraphics = Graphics.FromImage(objBmp);

// Set Background color

objGraphics.Clear(Color.White);

objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

objGraphics.DrawString(TextImage, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);

objGraphics.Flush();

return (objBmp);
}

Enjoy….

Posted in .Net, ASP.NET | Tagged: , , , , , , , , | Leave a Comment »

Add Watermark text on Image

Posted by Viral Sarvaiya on September 18, 2009


step 1. upload the image file
step 2. save the image and get the filename with the full the path where the image file will store.


dim pathname as string
// this "pathname" contains path with filename of the uploaded file with fileupload control.

Dim ImgStamp As New Bitmap(pathname)
Dim g As Graphics = Graphics.FromImage(ImgStamp)
g.SmoothingMode = SmoothingMode.AntiAlias
Dim y As Integer = 0
y = ImgStamp.Height - 85
Dim FSize As Double = ImgStamp.Height
Dim FWSize As Double = ImgStamp.Width
If FSize > 640 Then
'Set Position
FSize = ImgStamp.Width * 28 / 640
y = ImgStamp.Height / 1.1
ElseIf FWSize < 426 Then
'Set Position
FSize = ImgStamp.Width * 28 / 640
y = ImgStamp.Height / 1.1
Else
'Set Position
FSize = ImgStamp.Width * 28 / 640
y = ImgStamp.Height / 1.1
End If
g.DrawString("Put Water mark text here", New Font("font name", CType(FSize, Integer), FontStyle.Regular), Brushes.Azure, New Point(1, y))
ImgStamp.Save(path & imgName)

change the numbers according to the requirement, where you want the watermark text.

enjoy coding…..

Posted in ASP.NET | Tagged: , , , , , | Leave a Comment »

 
%d bloggers like this: