how to create jpg file from given string - vb.net

I have converted given string to JPG file format. But i need those image string in center. Iam getting output from leftside of rectangle.
Dim stext As String = "Testing"
Dim format As StringFormat = New StringFormat()
Dim MyRect As Rectangle = New Rectangle(0, 0, 400, 800)
Dim MyGraphics As Graphics = Me.CreateGraphics()
Dim MyImg As Image = New Bitmap(391, 132, MyGraphics)
Dim imageGraphics As Graphics = Graphics.FromImage(MyImg)
imageGraphics.FillRectangle(Brushes.White, MyRect)
format.Alignment = StringAlignment.Center
format.LineAli`enter code here`gnment = StringAlignment.Center
imageGraphics.DrawString("Testing", New Font("Times New Roman", 30, Drawing.FontStyle.Bold), Brushes.Black, RectangleF.op_Implicit(MyRect))
MyGraphics.DrawImage(MyImg, MyRect)
MyImg.Save(Destfilename & "/" & "test.jpg")
I have given this code output. But i need this string to in center position. How to do, Kindly help me through.
Output: https://drive.google.com/file/d/0B_nzYHWVJJ7Ka3N0V2NmRnl3UFk/view?usp=sharing

I hope this example could help you
Private Sub CenterTextAt(ByVal gr As Graphics, ByVal txt As _
String, ByVal x As Single, ByVal y As Single)
' Mark the center for debugging.
gr.DrawLine(Pens.Red, x - 10, y, x + 10, y)
gr.DrawLine(Pens.Red, x, y - 10, x, y + 10)
' Make a StringFormat object that centers.
Dim sf As New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
' Draw the text.
gr.DrawString(txt, Me.Font, Brushes.Black, x, y, sf)
sf.Dispose()
End Sub

Related

How to print multiple page using e.HasMorePages?

I want to print data from datagridview,
however when data exceed than one page it is missing. I try to use e.HasMorePages to print multiple pages. I try and search for example for 3 hours ago but unfortunately it does not work
Now when I click print, It is print only the same page without stopping.
Please help.
Private Sub PrintColorConsumption_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintColorConsumption.PrintPage
Dim x As Integer = 100
Dim y As Integer = 25
Dim header As Boolean = True
'draw headers
Dim j As Integer = 0
Do While (j < Me.DataGridView3.Columns.Count)
Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView3.Columns(j).Width, Me.DataGridView3.ColumnHeadersHeight)
Dim sf As StringFormat = New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
e.Graphics.FillRectangle(Brushes.LightGray, rect)
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (Me.DataGridView3.Columns(j).HeaderText) Is Nothing) Then
e.Graphics.DrawString(Me.DataGridView3.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 100
y = (y + Me.DataGridView3.ColumnHeadersHeight)
'draw rows
For Each row As DataGridViewRow In Me.DataGridView3.Rows
j = 0
Do While (j < Me.DataGridView3.Columns.Count)
Dim cell As DataGridViewCell
cell = row.Cells(j)
Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
Dim sf As StringFormat = New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (cell.Value) Is Nothing) Then
e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 100
y = (y + row.Height)
'----------------------New page----------------------------
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 20
End If
'-----------------------------------------------------------------
Next
End Sub
Printing requires you to maintain where you are within the data that you are printing for every PrintPage event. The way you have your variables declared they go out of scope and are set to their initial values every time the event is raised.
See if the following code, which wasn't tested, helps. Note that a variable was declared outside of the event handler to keep track of the current row.
Dim whRow As Integer = 0
Private Sub PrintColorConsumption_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintColorConsumption.PrintPage
Dim x As Integer = 100
Dim y As Integer = 25
Dim header As Boolean = True
'draw headers
Dim j As Integer = 0
Do While (j < Me.DataGridView3.Columns.Count)
Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView3.Columns(j).Width, Me.DataGridView3.ColumnHeadersHeight)
Dim sf As StringFormat = New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
e.Graphics.FillRectangle(Brushes.LightGray, rect)
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (Me.DataGridView3.Columns(j).HeaderText) Is Nothing) Then
e.Graphics.DrawString(Me.DataGridView3.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 100
y = (y + Me.DataGridView3.ColumnHeadersHeight)
'draw rows
For whRow = whRow To Me.DataGridView3.RowCount - 1
Dim drow As DataGridViewRow = Me.DataGridView3.Rows(whRow)
j = 0
Do While (j < Me.DataGridView3.Columns.Count)
Dim cell As DataGridViewCell
cell = drow.Cells(j)
Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
Dim sf As StringFormat = New StringFormat
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
e.Graphics.DrawRectangle(Pens.Black, rect)
If (Not (cell.Value) Is Nothing) Then
e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect, sf)
End If
x = (x + rect.Width)
j = (j + 1)
Loop
x = 100
y = (y + drow.Height)
'----------------------New page----------------------------
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 20
End If
'-----------------------------------------------------------------
Next
End Sub

convert group of points to create an image vb.net

I have a group of points.
I know how to draw the polygon in the panel
but now I don't know how to make it an image.
tried googling it but I can't find the answer
how can I make it an image?
is it possible?
Dim points(5) As Point
points(0) = New Point(50, 10)
points(1) = New Point(90, 30)
points(2) = New Point(75, 70)
points(3) = New Point(25, 70)
points(4) = New Point(10, 30)
points(5) = New Point(50, 10)
Dim img As New Bitmap(100, 100)
Dim gfx As Graphics = Graphics.FromImage(img)
gfx.DrawLines(Pens.Black, points)
Dim strFilename As String = "C:\Junk\Junk.png"
img.Save(strFilename, System.Drawing.Imaging.ImageFormat.Png)
gfx.Dispose()
img.Dispose()
Process.Start(strFilename)
Assuming you want the image to come from the Panel on which you've been drawing, then you can use DrawToBitmap:
Dim Bmp As New Bitmap(Panel1.Width, Panel1.Height)
Dim Clip As New Rectangle(New Point(0, 0), Panel1.Size)
Panel1.DrawToBitmap(Bmp, Clip)
Which you can then, for instance, set in another panel:
Panel2.BackgroundImage = Bmp
Or save:
Bmp.Save("MyPanel.bmp")

Saving image over open file, A Generic error occurred in GDI+

I'm having trouble with my code, I'm trying to make a simple captcha for my program. The first time I run the sub it works fine, I'm guessing because the file is not "open" in my program. The second time I get the GDI+ error. I'm pretty sure this is an issue with the bitmaps being locked the 2nd time around, I'm just a little stuck.
Sub generatePasswordImage(password As String)
'This is the image where we are going to write the text on it.
Dim stringMasterImageName As String = Application.StartupPath + "\MasterImage.jpg"
Dim bitmapImage As Bitmap = New System.Drawing.Bitmap(stringMasterImageName)
Dim bitmapMasterImage As Bitmap = New System.Drawing.Bitmap(bitmapImage, New Size(400, 150))
Dim graphicsMasterImage As Graphics = Graphics.FromImage(bitmapMasterImage)
'Set the alignment based on the coordinates
Dim stringformatWriteTextFormat As StringFormat = New StringFormat()
stringformatWriteTextFormat.Alignment = StringAlignment.Center
'Do some rotation effects
Dim Generator As System.Random = New System.Random()
Dim rotation As Integer = Generator.Next(-10, 5)
graphicsMasterImage.RotateTransform(rotation)
Dim centerImgWidth As Integer = Generator.Next(CInt(graphicsMasterImage.VisibleClipBounds.Size.Width / 3), CInt(graphicsMasterImage.VisibleClipBounds.Size.Width / 2))
Dim centerImgHiehgt As Integer = Generator.Next(CInt(graphicsMasterImage.VisibleClipBounds.Size.Height / 3), CInt(graphicsMasterImage.VisibleClipBounds.Size.Height / 2))
'Set the font color
Dim colorStringColor As Color = System.Drawing.Color.Black
graphicsMasterImage.DrawString(password, New Font("Tahoma", 14, FontStyle.Bold), _
New SolidBrush(colorStringColor), New Point(centerImgWidth, centerImgHiehgt), stringformatWriteTextFormat)
Dim stringOutPutFileName As String = Application.StartupPath + "\Pass.jpg"
bitmapMasterImage.Save(stringOutPutFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
bitmapMasterImage.Dispose()
bitmapImage.Dispose()
End Sub

Unable To Override OnDrawItem Correctly With OwnerDrawn Listview Using LargeIcon View

I am trying to finalize my custom Listview control with that uses LargeIcon view. I am trying to custom draw the Item within the OnDrawItem event.
So far I have the following code:
Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
Dim flags As TextFormatFlags
Dim subColour As Color = Color.Black
Dim subBackColour As Color = Color.Empty
Try
If Not (e.State And ListViewItemStates.Selected) = 0 Then
'Draw the background for a selected item.
e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, e.Bounds)
e.DrawFocusRectangle()
Else
'Draw the background for an unselected item.
e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Control, e.Bounds)
End If
e.DrawBackground()
'Draw the Icons
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
e.Item.ImageList.Draw(e.Graphics, New Point(20, 22), 0)
e.Graphics.ResetTransform()
e.DrawFocusRectangle()
'Draw the Text
flags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.Bottom
Dim rec As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width + 10, e.Bounds.Height + 10)
TextRenderer.DrawText(e.Graphics, e.Item.Text, Me.Font, rec, subColour, subBackColour, flags)
MyBase.OnDrawItem(e)
Catch ex As Exception
ErrorTrap(ex, "ListView_Stores: OnDrawItem()")
End Try
End Sub
However, when I run my code, it draws my Text and Icon correctly but I can't seem to get the Item Highlighting correct as per my picture below:
It's not highlighting correctly with any colour (just a dotted square) and it's not even highlighting the entire bounds of the object - it chops half way through the text.
Wondering if someone could assist or at least point me in the right direction.
Thanks
Okay, with a LOT of research and trial and error I have managed to achieve exactly what I was after by overriding the OnDraw event of the ListItem in my custom class. I'm unsure if it's the correct (or preferred) method, but I am happy with the outcome.
I ended up making use of the ColorMatrix method to overlay a 'blue' highlight color to my selected item. Then when unselected I simply set my ColorMatrix to nothing
My new revised code:
Protected Overrides Sub OnDrawItem(e As DrawListViewItemEventArgs)
Dim storeName_flags As New StringFormat
Dim storeCode_flags As New StringFormat
Dim matrixItems As Single()() = { _
New Single() {0, 0, 0, 0, 0}, _
New Single() {0, 0.6F, 0, 0, 0}, _
New Single() {0, 0, 3, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}}
Dim colorMatrix As ColorMatrix = New ColorMatrix(matrixItems)
Dim imgattr As ImageAttributes = New ImageAttributes
Dim bmp As Bitmap = New Bitmap(My.Resources.Store_Good)
Try
'Get StoreName and StoreNum from original e.Item.Text
Dim StoreDetail As String() = e.Item.Text.Split(New Char() {"|"c})
Dim StoreName As String = StoreDetail(0)
Dim StoreNum As String = StoreDetail(1)
'Declare Image Rectangle as the max size of the bitmap
Dim Image_Width As Integer = bmp.Width
Dim Image_Height As Integer = bmp.Height
Dim imgRect As New Rectangle(e.Bounds.X + ((e.Bounds.Width - Image_Width) / 2), e.Bounds.Y, Image_Width, Image_Height)
'Declare Text Rectangle
Dim textSize As SizeF = New SizeF(e.Graphics.MeasureString(StoreName, Me.Font, 100))
Dim textRect As New Rectangle(e.Bounds.X + ((e.Bounds.Width - textSize.Width) / 2), e.Bounds.Bottom - textSize.Height, textSize.Width + 1, textSize.Height)
e.Graphics.SmoothingMode = SmoothingMode.HighQuality
If e.Item.Selected Then
'Set the Image to use the 'blue' color matrix and highlight the text
imgattr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
e.Graphics.FillRectangle(System.Drawing.SystemBrushes.Highlight, textRect)
Else
'Turn off the color matrix and draw the default background
imgattr = Nothing
e.DrawBackground()
End If
'Draw the Image
e.Graphics.DrawImage(bmp, imgRect, 0, 0, Image_Width, Image_Height, GraphicsUnit.Pixel, imgattr)
storeCode_flags.Alignment = StringAlignment.Center
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
Dim rect2 As New Rectangle(e.Bounds.X + ((e.Bounds.Width - Image_Width) / 2) + 1, e.Bounds.Y + 15, Image_Width, Image_Height)
e.Graphics.DrawString(StoreNum, New Font(CustomFnt.Families(0), 24, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Black, rect2, storeCode_flags)
'Draw the Text
storeName_flags.Alignment = StringAlignment.Center
storeName_flags.LineAlignment = StringAlignment.Far
storeName_flags.FormatFlags = StringFormatFlags.FitBlackBox
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
e.Graphics.DrawString(StoreName, Me.Font, Brushes.Black, textRect, storeName_flags)
bmp.Dispose()
MyBase.OnDrawItem(e)
Catch ex As Exception
ErrorTrap(ex, "ListView_Stores: OnDrawItem()")
End Try
End Sub
Now instead of this:
I get this:

Split image across pages to print

I am trying to print an image in vb.net. The size of the image is dynamic, and is derived from a panel, so can span more than 1 page. For this, I am cropping the image and printing the first part, then recursively calling the procedure to print the next section. The first page prints okay, but the subsequent pages are blank, as is the image that is supposed to be on them.
800 is height of page, 1100 is width. All the save images are to pinpoint the problem: restimg.bmp comes up as blank, so the problem seems to be in the second using statement. I know very little about image manipulation, so simple terms and example please.
This is the code.
Sub recersive_print(ByVal WholeImg As Bitmap)
If WholeImg.Height > 800 Then
Dim CropRect As New Rectangle(0, 0, 1100, 800)
Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
Dim restofimg = New Bitmap(1100, WholeImg.Height - 800)
Dim restofingrect As New Rectangle(0, 0, restofimg.Height, restofimg.Width)
Using grp = Graphics.FromImage(CropImage)
grp.DrawImage(WholeImg, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
End Using
CropImage.Save("E:\cropped.bmp")
Using grp = Graphics.FromImage(restofimg)
grp.DrawImage(WholeImg, New Rectangle(0, CropRect.Height, restofimg.Width, restofimg.Height), restofingrect, GraphicsUnit.Pixel)
End Using
'img_filepath = Application.StartupPath & "\out" & Val(img_filepath) + 1 & ".bmp"
img_to_print = CropImage
'CropImage.Save(img_filepath)
PrintDocument1.Print()
'WholeImg.Dispose()
restofimg.Save("E:\Rest.bmp")
recersive_print(restofimg)
Else
img_to_print = WholeImg
img_to_print.Save("E:\out.bmp")
PrintDocument1.Print()
End If
End Sub
Thanks
EDIT: img_to_print is used in the following way
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
g.DrawImage(img_to_print, 5, 5)
End Sub
800 is width of page, 1100 is height. i believe you mean 1100 width and 800 height.
Sub recersive_print(ByVal WholeImg As Bitmap)
Static i As Integer = 0
i += 1
If WholeImg.Height > 800 Then
Dim CropRect As New Rectangle(0, 0, 1100, 800)
Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
Dim restofimg = New Bitmap(1100, WholeImg.Height - 800)
Dim restofingrect As New Rectangle(0, 0, restofimg.Width, restofimg.Height)
Using grp = Graphics.FromImage(CropImage)
grp.DrawImage(WholeImg, CropRect, CropRect, GraphicsUnit.Pixel)
End Using
CropImage.Save("E:\cropped" & i.ToString ".bmp")
Using grp = Graphics.FromImage(restofimg)
grp.DrawImage(WholeImg, restofingrect, New Rectangle(0, CropRect.Height, restofimg.Width, restofimg.Height), GraphicsUnit.Pixel)
End Using
restofimg.Save("E:\Rest" & i.ToString ".bmp")
recersive_print(restofimg)
Else
WholeImg.Save("E:\out.bmp")
End If
End Sub
valter