Convert SVG or Base30 to image file - vb.net

I'm trying to convert an SVG file to any format of image file.
I'm creating SVG file from base30 value.
Public Sub SaveSignature()
Dim B30 As New Base30Converter
Dim Img = B30.GetData("aQ4895d6d7j5h94840Z39baa6bfb6430Y4538d6a49c59db8863003240Z4374b7ek7j4a33Y255e8j5d6Z52Y3b2428385e9n1vd1Bfq6a4Z4Y3949456Z1e91wi2B2N1u2Eh1H9bY5j81ye1w1u8e5724983329d8pe1Ai1Nf1V1Uft9d5_7WZ266594c4g7gc36452542000Y2648a67334524112100Z1235766Y64Z9ah593420Y75g7d483401000Z34Y133Z332Y7e29262300Z43a3652Y26252556400Z2ig6h2200473720Y2263542006736424241000Z826010")
Dim svg = SVGConverter.ToSVG(Img)
Dim sw As New System.IO.StreamWriter("D:\Sign.svg", False)
sw.Write(svg)
sw.Close()
Dim bmp As Bitmap = renderFile("D:\Sign.svg")
Dim g As Graphics
g = Graphics.FromImage(bmp)
Using g
g.Clear(Color.White)
g.DrawImageUnscaled(bmp, 0, 0)
End Using
bmp.Save("D:\Sign.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
bmp.Save("D:\Sign.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
bmp.Save("D:\Sign.emf", System.Drawing.Imaging.ImageFormat.Emf)
End Sub
Public Function renderFile(filename As String) As Bitmap
Dim displaySize As System.Drawing.Size
displaySize.Width = 300
displaySize.Height = 200
Dim svgDoc As SvgDocument = SvgDocument.Open(filename)
Dim svgSize = svgDoc.GetDimensions()
If svgSize.Width = 0 Then
Throw New Exception("SVG does not have size specified. Cannot work with it.")
End If
Dim displayProportion = (displaySize.Height * 1.0F) / displaySize.Width
Dim svgProportion = svgSize.Height / svgSize.Width
Dim scalingFactor As Single = 0.0F
Dim padding As Integer = 10
If displayProportion > svgProportion Then
scalingFactor = ((displaySize.Width - padding * 2) * 1.0F) / svgSize.Width
Else
scalingFactor = ((displaySize.Height - padding * 2) * 1.0F) / svgSize.Height
End If
If scalingFactor < 0 Then
Throw New Exception("Viewing area is too small to render the image")
End If
Dim centeringX As Integer = Convert.ToInt16((displaySize.Width - (padding + CInt(svgDoc.Width) * scalingFactor)) / 2)
Dim centeringY As Integer = Convert.ToInt16((displaySize.Height - (padding + CInt(svgDoc.Height) * scalingFactor)) / 2)
svgDoc.Transforms = New SvgTransformCollection()
svgDoc.Transforms.Add(New SvgTranslate(padding + centeringX, padding + centeringY))
svgDoc.Transforms.Add(New SvgScale(scalingFactor))
svgDoc.Width = New SvgUnit(svgDoc.Width.Type, displaySize.Width)
svgDoc.Height = New SvgUnit(svgDoc.Height.Type, displaySize.Height)
Return svgDoc.Draw()
End Function
After all these process the image file left with empty file.
The base30 value I'm getting from a signature panel in asp application.

Related

UWP Print multiple images

I have modified Microsoft's printing example for printing multiple pictures, but the images are always aligned portrait, how can I align them landscape? According to Microsoft the pictures should be rotated automatically. The printing example is from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing.
Here is the code:
''' <summary>
''' Generates a page containing a photo.
''' The image will be rotated if detected that there is a gain from that regarding size (try to maximize photo size).
''' </summary>
''' <param name="photoNumber">The photo number.</param>
''' <param name="pageDescription">The description of the printer page.</param>
''' <returns>A task that will return the page.</returns>
Private Async Function GeneratePageAsync(photoNumber As Integer, pageDescription As PageDescription) As Task(Of UIElement)
Dim page As New Canvas() With {
.Width = pageDescription.PageSize.Width,
.Height = pageDescription.PageSize.Height
}
Dim viewablePage As New Canvas() With {
.Width = pageDescription.ViewablePageSize.Width,
.Height = pageDescription.ViewablePageSize.Height
}
viewablePage.SetValue(Canvas.LeftProperty, pageDescription.Margin.Width)
viewablePage.SetValue(Canvas.TopProperty, pageDescription.Margin.Height)
' The image "frame" which also acts as a viewport
Dim blnLandScape As Boolean
If MainPage._picsperprint = 2 OrElse MainPage._picsperprint = 6 OrElse MainPage._picsperprint = 8 OrElse MainPage._picsperprint = 32 OrElse MainPage._picsperprint = 128 Then
blnLandScape = True
ElseIf MainPage._picsperprint > 0 Then
blnLandScape = False
'PrintPageSqareNumbers(e, PrintPage)
ElseIf MainPage._picsperprint = -1 Then
'PrintPageSized(e, PrintPage)
End If
' Return an async task that will complete when the image is fully loaded.
Dim blnSuccess As Boolean = False
Dim pos As Integer = (photoNumber - 1) * MainPage._picsperprint
For i As Integer = 0 To _Rows - 1
For ii = 0 To _Columns - 1
If pos + i * (_Columns) + ii >= MainPage.checkedItems.Count Then Exit For
Dim height As Integer = pageDescription.PictureViewSize.Height
Dim width As Integer = pageDescription.PictureViewSize.Width
height = height / _Rows
width = width / _Columns
Dim photoView As New Grid() With {
.Width = width,
.Height = height
}
' Center the frame.
Dim Left As Integer = (viewablePage.Width - photoView.Width * _Columns) / 2
Dim Top As Integer = (viewablePage.Height - photoView.Height * _Rows) / 2
Top += height * i
Left += width * ii
photoView.SetValue(Canvas.LeftProperty, Left)
photoView.SetValue(Canvas.TopProperty, Top)
Dim position As Integer = (pos + i * (_Columns) + ii)
Dim Item As ImgListItem = GetItemOfPosition(position)
Dim bitmap As WriteableBitmap = Await Item.LoadBitmapAsync(False)
If bitmap IsNot Nothing Then
Dim image As New Image() With {
.Source = bitmap,
.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left,
.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top
}
' Use the real image size when croping or if the image is smaller then the target area (prevent a scale-up).
If photoScale = Scaling.Crop OrElse (bitmap.PixelWidth <= width AndAlso bitmap.PixelHeight <= height) Then
image.Stretch = Stretch.None
image.Width = bitmap.PixelWidth
image.Height = bitmap.PixelHeight
End If
' Add the newly created image to the visual root.
blnSuccess = True
photoView.Children.Add(image)
viewablePage.Children.Add(photoView)
End If
Next ii
Next i
If blnSuccess Then
page.Children.Add(viewablePage)
' Return the page with the image centered.
Return page
Else
Return Nothing
End If
End Function
There is not automatic rotation. The image must be rotated (transformed) in Item.LoadBitmapAsync(False).

Adding images of different sizes into a listview

I have a listview to which i add images of different sizes, eg. 123x23, 23,43, and so on..
How do i go on about this problem. I know listview has a tilesize property but that sets the general size of all the tiles
Tried with an imagelist, changing the imagelist imagesize doesnt help either... Heres the code i use to add the images to the listbox
The imglist in the code is an imagelist to which all the required images are loaded.
Private Sub frm_load_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.ListViewEx1.LargeImageList = imglist
For i = 0 To imglist.Images.Count - 1
Dim x = Form1.ListViewEx1.Items.Add(New ListViewItem("", i))
x.Tag = imglist.Images.Keys(i).ToString
Next
Form1.lbl_status1.Text = "Image Count: " & Form1.ListViewEx1.Items.Count
End sub
I had the same problem.
I found this and that works for me: Click here
Public Sub LoadImageList(ByVal ImagePath As String, ByVal Key As String)
Dim picImage As Image = Nothing
Dim final_Bitmap As Bitmap = Nothing
Dim org_Image As Bitmap = Nothing
If File.Exists(ImagePath) Then
picImage = Image.FromFile(ImagePath)
'********************* Drawing the Image in proportion to the imagelist Size Here ****************
Dim proportion As Integer = 0
Dim startx As Decimal = 0
Dim startY As Decimal = 0
Dim drawwidth As Decimal = 0
Dim drawheight As Decimal = 0
org_Image = New Bitmap(picImage)
final_Bitmap = New Bitmap(ImageList1.ImageSize.Width, ImageList1.ImageSize.Height)
Dim gr As Graphics = Graphics.FromImage(final_Bitmap)
Dim factorscale As Decimal
factorscale = org_Image.Height / org_Image.Width
drawwidth = final_Bitmap.Width
drawheight = final_Bitmap.Width * factorscale
If drawheight > final_Bitmap.Height Then
proportion = 1
factorscale = org_Image.Width / org_Image.Height
drawheight = final_Bitmap.Height
drawwidth = final_Bitmap.Height * factorscale
End If
startx = 0
startY = final_Bitmap.Height - drawheight
gr.DrawImage(org_Image, startx, startY, drawwidth, drawheight)
ImageList1.Images.Add(Key, final_Bitmap)
org_Image.Dispose()
final_Bitmap.Dispose()
'************************** End Loading the Image****************
End If
End Sub

Changing the area of a pixel search

I have been working on an app to search for 3 pixels in a certain area then play a sound. It currently works however I wish to reduce the zone of which it searches from the whole screen to a rectangular area on my screen, this is to save memory usage of the app, here's the code that works
Private Sub dcdTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dcdTimer.Tick
Dim screensize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenshot As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim ge As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenshot)
Dim pointx As Integer = 1
Dim pointy As Integer = 1
ge.CopyFromScreen(New Point(0, 0), New Point(0, 0), screensize)
looking = True
Try
While looking = True
Dim atpoint As Color = screenshot.GetPixel(pointx, pointy)
Dim reckcol1 As Color = Color.FromArgb(a, r, g, b) 'first pixel colour
Dim reckcol2 As Color = Color.FromArgb(a1, r1, g1, b1) 'second pixel colour
Dim reckcol3 As Color = Color.FromArgb(a2, r2, g2, b2) 'third pixel colour
If atpoint = reckcol1 Then 'Matches 3 pixels
Dim testPoint As Color = screenshot.GetPixel(pointx, pointy + 10)
If testPoint = reckcol2 Then
Dim testPoint2 As Color = screenshot.GetPixel(pointx + 10, pointy + 10)
If testPoint2 = reckcol3 Then
My.Computer.Audio.Play(sounddir + "Found.wav")
looking = False
Sleep(15000)
pointx = 0
pointy = 0
End If
End If
End If
pointy = pointy + 1
If pointy = My.Computer.Screen.Bounds.Height Then
pointy = 0
pointx = pointx + 1
End If
End While
Catch ex As Exception
End Try
End Sub
I tried to use this code as suggested:
Dim pointofY As New Point(500, 400)
Dim screensize As New Size(400, 200)
Dim screenshot As New Rectangle(pointofY, screensize)
Dim ge As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screenshot)
However, using a rectangle does not allow me to use getpixel or use ge.copyfromscreen

Image Recognition between 2 images vb.net Lockbits

I was in the process of using Pixel to do my search between a large image and a small image. I got that one to successfully work but am having trouble getting the lockbits version to do what I want it to do.
Fist I want to loop through the large image finding the first pixel of the smaller image inside it. Then once finding the first pixel to do a math equation to tell me what percentage of accuracy it got off it. In doing so if it meets the criteria to move my mouse to that location on the screen at the first pixel (top most left) of that small image inside the large image. Which works with a great rate of success and is extremely fast compared to GetPixel format.
Problem Cannot get location on the large image of where the beginning Pixel of the small image is and move the mouse to that location. Below is the code:
Try
Dim ifnd As Boolean = False
Dim PreviousX As Integer
Dim PreviousY As Integer
PreviousX = MousePosition.X
PreviousY = MousePosition.Y
Dim MatchCount As Integer = 0
Dim oX As Integer = 0
Dim oi As Integer = 0
Dim iX As Integer = 0
Dim iY As Integer = 0
Dim bmp_original As Bitmap
Dim ImG As Bitmap
ImG = PictureBox2.BackgroundImage
bmp_original = ImG
Dim bmp_large As Bitmap
Dim SmG As Image
SmG = PictureBox1.BackgroundImage
bmp_large = SmG
Dim bmg As Bitmap
'large image
ImG = BBt
'small image
bmg = Abt
Thread.Sleep(1000)
'large image
If BBt Is Nothing Then
Dim xbit As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(xbit)
BBt = xbit
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size)
g.Dispose()
Thread.Sleep(2000)
End If
'small image
PictureBox5.BackgroundImage = Abt
'large image
PictureBox6.BackgroundImage = BBt
'For value As Integer = 0 To 5
'For value As Integer = 10 To 0 Step -2
Thread.Sleep(1000)
'original image
Dim oRect As New Rectangle(0, 0, bmg.Width, bmg.Height)
Dim oBmpData As BitmapData = bmg.LockBits(oRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim oPtr As IntPtr = oBmpData.Scan0
Dim oPixels(99) As Integer
Dim oMaxPix As Integer = bmg.Width + bmg.Height
Marshal.Copy(oPtr, oPixels, 0, oMaxPix)
Dim smWidth As Integer
smWidth = bmg.Width - 1
'small image
PictureBox3.BackgroundImage = bmg
'large image
Dim lRect As Rectangle = New Rectangle(0, 0, bmp_large.Width, bmp_large.Height)
Dim lBmpData As BitmapData = ImG.LockBits(lRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim lPtr As IntPtr = lBmpData.Scan0
Dim PixelCount As Integer = ImG.Width * ImG.Height
Dim lPixels(PixelCount - 1) As Integer
Marshal.Copy(lPtr, lPixels, 0, PixelCount)
'large image
PictureBox4.BackgroundImage = ImG
Dim MathScore As Integer
Dim MaxScore As Integer = bmg.Height
'beginning of Marshal Loop
For i As Integer = 0 To lPixels.GetUpperBound(0)
If oPixels(0) = lPixels(i) Then
'we have a match for top left pixel - so compare the other pixels
Dim PixelsToLeft As Integer = (i Mod ImG.Width) - 1 'pixels to left of 10by10 section of large image
Dim PixelsToRight As Integer = ImG.Width - PixelsToLeft - smWidth 'pixels to right of 10by10 section of large image
Dim d As Integer = PixelsToLeft + PixelsToRight 'array distance between right edge of 10by10 section and left edge of next row
For y As Integer = 0 To 9
For x As Integer = 0 To 9
Dim oIndex As Integer = (y * 10) + x
Dim lIndex As Integer = (i + (y * (d + smWidth))) + x
If oPixels(oIndex) = lPixels(lIndex) Then
MathScore = MathScore + 1
xx = oPixels(0) + 2
yy = lPixels(i) + 3
SetCursorPos(xx, yy)
End If
Next
Next
If MathScore >= Val(MaxScore / 2.5) Then
SetCursorPos(xx, yy)
Dim percent As String
Dim myDec As Decimal
'inttemp = (intData2 * 100) / intData1
myDec = Val((MathScore * 100) / MaxScore)
myDec = FormatNumber(myDec, 0)
percent = myDec & "%"
Label16.Text = "Match Score: " & percent
Label17.Text = "Math Score: " & MathScore & " out of " & MaxScore
Me.ToolStripStatusLabel2.Text = "Completed"
Me.Button4.Enabled = True
GoTo Foundit
End If
End If
Next
PictureBox1.Image = (Abt)
PictureBox2.Image = (BBt)
ImG.UnlockBits(oBmpData)
bmg.UnlockBits(lBmpData)
Foundit:
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Catch
End Try
Now if I could figure out how to get the mouse to move on the screen then I would have figured it out. Unfortunately I have been working on this for several days without any success. If you could help I would greatly appreciate it. Thank you in advance.
to move the mouse do this
dim a as new point
a.x = "the number you want"
a.y = "the number you want"
windows.forms.cursor.position = a

VB.NET - Creating, resizing and appending images

I have two bmp files:
footer.bmp: 200 x 200
product.bmp: 1000 x 1000
I want to create a new bmp file with 200 x 500:
Append the footer.bmp into the bottom of the new image - position (0, 300)
Resize the product.bmp to 200 x 300 and position into (0, 0)
How do I do this using VB.NET?
Dim oBitmap As New Bitmap(200, 500)
Dim oGraphics As Graphics
oGraphics = Graphics.FromImage(oBitmap)
... ?
Dim Path As String = "C:\Delivery\"
Dim Height As Integer = 400
Using oFooter As System.Drawing.Image = Drawing.Image.FromFile(Path + "Footer.png")
Dim Width As Integer = oFooter.Width
Using oBitmap As New Bitmap(Width, Height)
Using oGraphic As Graphics = Graphics.FromImage(oBitmap)
Using oBrush As New SolidBrush(Color.White)
oGraphic.FillRectangle(oBrush, 0, 0, Width, Height)
End Using
oGraphic.DrawImage(oFooter, 0, 300)
Using oProduto As System.Drawing.Image = Drawing.Image.FromFile(Path + "Produto.png")
Dim pWidth As Integer = oProduto.Width
Dim pHeight As Integer = oProduto.Height
If pWidth > Width Then
pHeight = CInt(pHeight * Width / pWidth)
pWidth = Width
End If
If pHeight > Height Then
pWidth = CInt(pWidth * Height / pHeight)
pHeight = Height
End If
Dim x As Integer = CInt((Width - pWidth) / 2)
Dim y As Integer = CInt((Height - oFooter.Height - pHeight) / 2)
oGraphic.DrawImage(oProduto, x, y, pWidth, pHeight)
End Using
oBitmap.Save(Path + "Final.jpg", Imaging.ImageFormat.Jpeg)
End Using
End Using
End Using