Code Simplified – Viral Sarvaiya

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

Posts Tagged ‘Bitmap’

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….

Advertisements

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

Image Resize or create thumb image

Posted by Viral Sarvaiya on August 4, 2009


The project shows the resizing of the image, means create the thumb file of the actual image, here i show how to create the thumb as well as Medium sized file.

first create the class file named “GeneralMethods.vb”

Public Class GeneralMethods
Public Shared Function CreateThumbImage(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
Dim bmpOut As Bitmap = Nothing
Try
Dim oBMP As New Bitmap(imgPhoto)
Dim oFormat As Imaging.ImageFormat = oBMP.RawFormat
Dim NewWidth As Integer = 0
Dim NewHeight As Integer = 0

'*** If the image is smaller than a thumbnail just return it

If oBMP.Width <= Width AndAlso oBMP.Height <= Height Then
Return imgPhoto
End If

Dim per As Double
If oBMP.Width > Width Or oBMP.Height > Height Then
If oBMP.Width > oBMP.Height Then
per = (100 * Width) / oBMP.Width
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
Else
per = (100 * Height) / oBMP.Height
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
End If

If NewHeight > Height Or NewWidth > Width Then
If NewWidth > NewHeight Then
per = (100 * Width) / NewWidth
Else
per = (100 * Height) / NewHeight
End If
NewWidth = (NewWidth * per) / 100
NewHeight = (NewHeight * per) / 100
End If

End If

bmpOut = New Bitmap(NewWidth, NewHeight)

Dim g As Graphics = Graphics.FromImage(bmpOut)

g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.High
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

Dim codec As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders(1)
Dim eParams As Drawing.Imaging.EncoderParameter = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1)

g.FillRectangle(Brushes.White, 0, 0, NewWidth, NewHeight)

g.DrawImage(oBMP, 0, 0, NewWidth, NewHeight)

oBMP.Dispose()

Catch
Return Nothing
End Try
Return bmpOut
End Function

Imports this 2 namesapces,
Imports System.IO
Imports System.Drawing

in the html side, take a fileupload control named “file1” and one button.
on the click of the button write the following code


If file1.HasFile Then
extension = Path.GetExtension(file1.PostedFile.FileName).ToLower()
SyncLock (objLock)
'get the max id from the database
maxId = (objclass.GetMaxId())
filename = maxId & extension

strImagePath = rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH") & filename
End SyncLock
If Not Directory.Exists(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH")) Then
Directory.CreateDirectory(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH"))
End If

objprice.photograph = filename

file1.PostedFile.SaveAs(strImagePath)

If File.Exists(strImagePath) Then
Dim actualImage As Drawing.Image
Dim mediumImage As Drawing.Image = Nothing
Dim thumbImage As Drawing.Image = Nothing

actualImage = Drawing.Image.FromFile(strImagePath)

'------------- Medium Image --------------------
Dim mediumWidth As Integer = ConfigurationManager.AppSettings("MEDIUM_WIDTH")
Dim mediumHeight As Integer = ConfigurationManager.AppSettings("MEDIUM_HEIGHT")
mediumImage = GeneralMethods.CreateThumbImage(actualImage, mediumWidth, mediumHeight)

If mediumImage.Width > mediumWidth Then
mediumImage = mediumImage.GetThumbnailImage(mediumWidth, mediumImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage.Height > mediumHeight Then
mediumImage = mediumImage.GetThumbnailImage(mediumImage.Width, mediumHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage IsNot Nothing Then
mediumImage.Save(rootPath & ConfigurationManager.AppSettings("MEDIUM_PATH") & filename)
End If
mediumImage.Dispose()
actualImage.Dispose()
'-------------  End Medium  --------------------

actualImage = Drawing.Image.FromFile(strImagePath)
'--------------- Thumb Image -----------------
Dim thumbWidth As Integer = ConfigurationManager.AppSettings("THUMB_WIDTH")
Dim thumbHeight As Integer = ConfigurationManager.AppSettings("THUMB_HEIGHT")
thumbImage = GeneralMethods.CreateThumbImage(actualImage, thumbWidth, thumbHeight)

If thumbImage.Width > thumbWidth Then
thumbImage = thumbImage.GetThumbnailImage(thumbWidth, thumbImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage.Height > thumbHeight Then
thumbImage = thumbImage.GetThumbnailImage(thumbImage.Width, thumbHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage IsNot Nothing Then
thumbImage.Save(rootPath & ConfigurationManager.AppSettings("THUMB_PATH") & filename)
End If
thumbImage.Dispose()
'------------- End Thumb Image -----------------

actualImage.Dispose()
End If

End If
Private Function ThumbnailCallback() As Boolean
Return False
End Function

according to the requirements set the application setting of the web.config.

hope this will help you

enjoy coding…..

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

 
%d bloggers like this: