Write Text in image
My New Task,
Feature which allow user to select images with single click with sequence number shown on image as part of selection.
Public Sub ImageProcessing(ByVal Counter As Integer, ByVal RowIndex As Integer, ByVal ColIndex As Integer, Optional ByVal ArrayImage() As Byte = Nothing)
Dim byteArrayIn() As Byte
Dim ms As MemoryStream
Dim sourceImage As Image
If IsNothing(ArrayImage) = True Then
byteArrayIn = grdStudent.Item(RowIndex, ColIndex).CellValue
Else
byteArrayIn = ArrayImage
End If
ms = New MemoryStream(byteArrayIn)
sourceImage = Image.FromStream(ms)
'Resize Image so that text Can have shown proper, as images are of different size text ratio can't be maintained
ms = New MemoryStream()
sourceImage = ResizeImage(sourceImage, New Size(800, 650))
'Fade Image
Dim graphics As Graphics = graphics.FromImage(sourceImage)
Dim pLight As New Pen(Color.FromArgb(175, 224, 224, 224), sourceImage.Width * 2) ' //create mask with blended alpha value and chosen color as pen
graphics.DrawLine(pLight, -1, -1, sourceImage.Width, sourceImage.Height) ' //apply created mask to graphics object
graphics.Save() '//save created graphics object and modify image object by that
graphics.Dispose() ' //dispose graphics object
sourceImage.Save(ms, ImageFormat.Jpeg)
Dim bm As New Bitmap(ms)
Dim gr As Graphics = graphics.FromImage(bm)
gr.DrawImage(sourceImage, 0, 0)
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
Dim sf As New StringFormat()
sf.Alignment = StringAlignment.Center
gr.DrawString(Counter, New Font("Verdana", 50, FontStyle.Bold), Brushes.Blue, 60, 520, sf)
ms = New MemoryStream()
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
grdStudent.IgnoreReadOnly = True
grdStudent.Item(RowIndex, ColIndex).CellValue = ms.ToArray
grdStudent.Refresh()
grdStudent.IgnoreReadOnly = False
bm.Dispose()
gr.Dispose()
End Sub
Re size of image is required as all image may not of same size. So to maintain the Text ratio. Image has to be re sized to common width and height.
Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End If
Dim newImage As Image = New Bitmap(newWidth, newHeight)
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage
End Function
Comments
Post a Comment