how can i convert a bitmap to mat VB.net - vb.net

i am working in a project where i have to convert a bitmap image from a screenshot to a mat format directly in order to use it with engu opencv. it only works when i save the image and call it with New Mat() funtion.
Public Function ScreenShot() As Bitmap
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width,
My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
screenGrab.Save("C:\Users\Desktop\test.png")
imgOriginal = New Mat("C:\Users\Desktop\test.png")
screengrab is the bitmap but how can i use it directly as a mat format without saving it?.
i think this saving operation could damage the hard drive because it is saving and reading this every second.

Related

Create a new picture along the GraphicsPath

Is there some way to copy a GraphicsPath and the enclosed figure into a new picture?
I have the rectangle, the points of the GraphicsPath available. The path is definitely in the rectangle.
I already googled but the result is poor. So far, I can only copy a certain area (rectangle) into a new picture, see source code.
Using Extracted As Bitmap = New Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
Using Grp As Graphics = Graphics.FromImage(Extracted)
Grp.DrawImage(Picture1, 0, 0, rect, GraphicsUnit.Pixel)
End Using
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
Extracted.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
I have found something here:
This is the solution translated into VB.Net and with Option Strict On and Option Infer Off.
Using bmpSource As Bitmap = New Bitmap(Form1.Pfad_Bild)
Dim rectCutout As RectangleF = gp.GetBounds()
Using m As Matrix = New Matrix()
m.Translate(-rectCutout.Left, -rectCutout.Top)
gp.Transform(m)
End Using
Using bmpCutout As Bitmap = New Bitmap(CInt(rectCutout.Width), CInt(rectCutout.Height))
Using graphicsCutout As Graphics = Graphics.FromImage(bmpCutout)
graphicsCutout.Clip = New Region(gp)
graphicsCutout.DrawImage(bmpSource, CInt(-rectCutout.Left), CInt(-rectCutout.Top))
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
bmpCutout.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
End Using
End Using

How to rotate an Image inside PictureBox by 20 degrees VB 2015?

Say, i have a PictureBox1 in my Form. That PictureBox have an Image in it, and I want to rotate it. Actually, I've learn how to rotate the image by 90,180,270.... degree. But how can I rotate it by 20 degrees or 45 degrees?
This is what I've learn
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
My solution:
Public Function RotateImage(ByRef image As Image, ByVal offset As PointF, ByVal angle As Decimal) As Bitmap
If image Is Nothing Then
Throw New ArgumentNullException("image")
End If
''create a new empty bitmap to hold rotated image
Dim rotatedBmp As Bitmap = New Bitmap(image.Width, image.Height)
'Dim rotatedBmp As Bitmap = New Bitmap(image)
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution)
''make a graphics object from the empty bitmap
Dim g As Graphics = Graphics.FromImage(rotatedBmp)
''Put the rotation point in the center of the image
g.TranslateTransform(offset.X, offset.Y)
''rotate the image
g.RotateTransform(angle)
''move the image back
g.TranslateTransform(-offset.X, -offset.Y)
''draw passed in image onto graphics object
'g.DrawImage(image, New PointF(0, 0))
g.DrawImage(image, offset)
Return rotatedBmp
End Function

How to Re-size image on File Upload

I am trying to resize the image that is being uploaded using FileUpload control in ASP.net using VB.NET
I am using a file upload code:
If FileUpload1.HasFile Then
Dim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), newfile)
FileUpload1.SaveAs(theFileName)
End If
How do I resize the image to width:270px height:307px?
Well, I hope this help :
Dim bmp As New Bitmap(FileUpload1.PostedFile.InputStream)
Dim newImage = New Bitmap(newWidth, newHeight)
Using g = Graphics.FromImage(newImage)
g.DrawImage(bmp, 0, 0, newWidth, newHeight)
End Using

Invert or Flip Text in RDLC report

Okay, I've learned a bit more and have rephrased my question. I've got a need to flip or invert text 180 degrees (so it appears upside-down) on a RDLC report. I have some custom VB code that takes the text, converts it to a bitmap, then flips the rotates the canvas 180 degrees. The effect of this makes the text look a bit.. dithered... or fuzzy. It's not a sharp font anymore. The problem I'm experiencing is I'm using a special TTF Barcode font that creates something a scanner can read. When I flip the barcode font, the fuzziness isn't good since the barcode lines are so close together and the scanner cannot read it. Here's the code:
Function LoadImage(ByVal sImageText as String, iRotationAngle as Integer, ByVal sFontName as String, iFontSize as Integer)
Dim bmpImage As New Drawing.Bitmap(1, 1)
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
'// Create the Font object for the image text drawing.
Dim MyFont As New Drawing.Font(sFontName, iFontSize) ', System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
'// Create a graphics object to measure the text's width and height.
Dim MyGraphics As Drawing.Graphics = Drawing.Graphics.FromImage(bmpImage)
'// This is where the bitmap size is determined.
iWidth = MyGraphics.MeasureString(sImageText, MyFont).Width
iHeight = MyGraphics.MeasureString(sImageText, MyFont).Height
'// Create the bmpImage again with the correct size for the text and font.
bmpImage = New Drawing.Bitmap(bmpImage, New Drawing.Size(iWidth, iHeight))
'// Add the colors to the new bitmap.
MyGraphics = Drawing.Graphics.FromImage(bmpImage)
MyGraphics.Clear(Drawing.Color.White)
MyGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
MyGraphics.TranslateTransform(iWidth,iHeight)
MyGraphics.RotateTransform(iRotationAngle)
MyGraphics.DrawString(sImageText, MyFont, New Drawing.SolidBrush(Drawing.Color.Black), 0, 0)
MyGraphics.Flush()
Dim stream As IO.MemoryStream = New IO.MemoryStream
Dim bitmapBytes As Byte()
'Create bitmap
bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
bitmapBytes = stream.ToArray
stream.Close()
bmpImage.Dispose()
Return bitmapBytes
End Function
I really don't know why there's not a built-in way to just flip text. It'll let me reverse it left-to-right. Ridiculous.
Thanks

How do I draw a circular gradient?

How do I draw a circular gradient like this in vb.net?
Check out this great page. The code in the article is in C#. Here is a VB.NET port of the code you're interested in and updated for a rectangular fill (based on the article's triangle fill sample):
Dim pgb As New PathGradientBrush(New Point() { _
New Point(0, 0), _
New Point(0, Me.ClientRectangle.Height), _
New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height), _
New Point(Me.ClientRectangle.Width, 0)})
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
pgb.Dispose()
Here's another possible solution:
Dim pth As New GraphicsPath()
pth.AddEllipse(Me.ClientRectangle)
Dim pgb As New PathGradientBrush(pth)
pgb.SurroundColors = New Color() {Color.Red}
pgb.CenterColor = Color.Gray
e.Graphics.FillRectangle(pgb, Me.ClientRectangle)
Note that this last code snippet will draw a circle bounded inside of a rectangle. If you want the circular gradient to fill the entire rectangle you'll have to calculate a larger elliptic path with a larger rectangle.