Code Simplified – Viral Sarvaiya

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

Posts Tagged ‘image’

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 »

Set Source on Image from code in Silverlight

Posted by Viral Sarvaiya on January 11, 2011


Hi

this is the first post of year 2o11

here I demonstrate how to set source of the image from code in silverlight

basically we define source of image from XAML file as below


<Image x:name="imgPhoto" Source="http://www.viralsarvaiya.wordpress.com/imgphoto.jpg" />

so how can we define this source from code file.


string imgurl = " http://www.viralsarvaiya.wordpress.com/imgphoto.jpg ";

ImageSourceConvertor convertor = new ImageSourceConvertor();

imgPhoto.Source = (ImageSource) convertor.ConvertFromString(imgurl);

here we use ImageSourceConvertor but u can also use BitmapImage with an Uri class as below,


Uri uri = new Uri("http://www.viralsarvaiya.wordpress.com/imgphoto.jpg", Urikind.Absolute);

ImageSource imgs = new ImageSource(uri);

imgPhoto.Source = imgs;

with this you can define source of the image from source.

Enjoy…

Posted in Silverlight | 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 »

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 »

Image Upload and Resize

Posted by Viral Sarvaiya on March 13, 2009


Lets assume we have a FileUpload control (named File1) and a Button control ( imgExtSubmit) and we’re going to upload the image when we click our button. The simplest method of uploading our file might look something like this :

for this you have to add the Namespace :

Imports System.Drawing
Imports System.Drawing.Imaging

Protected Sub imgExtSubmit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgExtSubmit.Click

If File1.HasFile Then

Dim file As String = System.DateTime.Now.Millisecond

If File1.FileName <> “” Then
imgName = GeneralMethods.GetCallbackPath(File1.FileName, file)
File1.SaveAs(Server.MapPath(“../images/”) & imgName)
GeneralMethods.Thmbimages(Server.MapPath(“../images/”) & imgName, Server.MapPath(“../thumb/”), imgName, 116, 76)
Else
imgName = “noimages.gif”
End If

End If

after this coding make a public class named : GeneralMethods.vb

Public Class GeneralMethods

Public Shared Function GetCallbackPath(ByVal returnValue As String, ByVal Fileno As String) As String
Dim fileName As String = System.IO.Path.GetFileName(returnValue)
Dim strfile() As String = fileName.Split(“.”)
fileName = strfile(0) & Fileno & “.” & strfile(1)
If fileName.Length > 10 Then
Dim i As Integer = CInt(fileName.Length) – 10
fileName = fileName.Substring(i, 10)
i = fileName.Length
End If
Return fileName
End Function

Public Shared Function Thmbimages(ByVal MainPath As String, ByVal ThmbPath As String, ByVal Filename As String, ByVal Passwidth As String, ByVal Passheight As String, Optional ByVal FixFlag As Integer = 0) As String
Dim width, height As Integer
Dim pathname As String
Dim inp As New IntPtr
Dim orginalimg, thumb As System.Drawing.Image

orginalimg = orginalimg.FromFile(MainPath)
width = orginalimg.Width
height = orginalimg.Height

If FixFlag <> 0 Then
width = Passwidth
height = Passheight
Else
If width > Passwidth Or height > Passheight Then
Dim per As Double
If width > height Then
per = (100 * Passwidth) / width
width = (width * per) / 100
height = (height * per) / 100
Else
per = (100 * Passheight) / height
width = (width * per) / 100
height = (height * per) / 100
End If
End If
End If
thumb = orginalimg.GetThumbnailImage(width, height, Nothing, inp)

pathname = ThmbPath & Filename

If File.Exists(pathname) Then
File.Delete(pathname)
End If

thumb.Save(pathname)
thumb.Dispose()
orginalimg.Dispose()
Return Filename
End Function

End Class

this function directly save the actual file to the image folder and make a small image and put it to the THUMB folder.

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

 
%d bloggers like this: