Capture Snapshot image from video in AxWindowsMediaPlayer in vb.net - vb.net

I am looking for a function to take a screenshot from an embedded Windows Media Player control in a VB.NET Windows form. I am currently using the following function; it works fine, but the problem is that x and y are different on each screen so it would be better if I could use a function in the AxWindowsMediaPlayer control itself, like .Capture() or a similar method.
Private Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As Bitmap
Dim Img As New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(X, Y, 0, 0, Img.Size)
g.Dispose()
Return Img
End Function
Dim bmp As Bitmap = TakeImage(x, y - 20, AxWindowsMediaPlayer1.Width, AxWindowsMediaPlayer1.Height)
bmp.Save("E:\pics\" & i.ToString & ".jpg", Drawing.Imaging.ImageFormat.Jpeg)

You could try Ctl.PointToScreen() to get an absolute screen point for CopyFromScreen.
I would get the AxWMP.PointToScreen in the procedure each time since the form could move. Pass them to CopyFromScreen and see if that works. I am not sure if it expects X,Y relative to the app, form or what but Screen, does mean screen.

Related

VB.NET Modify a Bitmap in a function Call

I'm trying to modify a bitmap in a function call.
Without the function call I can do
'MyBitMap - an existing bitmap of what I want to modify
Using NewBitMap as BitMap = MyBitMap.Clone 'make copy
Dim G as Graphics = Graphics.FromImage(MyBitMap) 'Draw graphics to MyBitMap
G.Clear(color.white) 'Clear image
G.DrawImage(NewBitMap, -10, 0) 'Shift Image to left 10
PictureBox1.Image = MyBitMap
End Using
works fine no memmory overflow or anything
In a sub it works fine
Sub BMScroll_S(BM as BitMap, dx as integer, dy as integer)
using BMtemp as BitMap = BM.Clone
Dim G as Graphics = Graphics.FromImage(BM)
G.Clear(color.white)
G.DrawImage(BMTemp, dx, dy)
End Using
End Sub
Call BMScroll_S(MyBitMap, -10, 0)
PictureBox1.Image = MyBitMap
works fine, but if I try to create a Function to return a Bitmap
Function BMScroll_F(BM as BitMap, dx as integer, dy as integer) as Bitmap
BMScroll_F = New Bitmap(BM)
Using BMtemp As Bitmap = BM.Clone
Dim G As Graphics = Graphics.FromImage(BMScroll_F)
G.Clear(Color.White)
G.DrawImage(BMtemp, dx, dy)
BM.Dispose()
End Using
End Function
MyBitMap=BMScroll_F(MyBitMap, -10, 0)
PictureBox1.Image = MyBitMap
here I have a Memory Leak and after more and more iterations, it will crash.
I suppose in the function call you are returning a bitmap as well as the fact that BitMaps are passed ByRef so they will continue to exist. I thought BM.Dispose might get rid of it - but it doesn't. I not quite sure how to solve my memory leak (if it is in fact due to my supposition). Granted, I could continue on with the Subroutine but I'd like to know how to solve this problem any way. Any help would be appreciated.

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

Dropping shadow on WinForm distorts interface

I'm using the following code to create a Windows7 style drop shadow effect on my WinForms:
<DllImport("dwmapi.dll", PreserveSig:=True)> _
Private Shared Function DwmSetWindowAttribute(hwnd As IntPtr, attr As Integer, ByRef attrValue As Integer, attrSize As Integer) As Integer
End Function
<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(hWnd As IntPtr, ByRef pMarInset As Margins) As Integer
End Function
Private Function CreateDropShadow() As Boolean
Try
Dim val As Integer = 2
Dim ret1 As Integer = DwmSetWindowAttribute(Me.Handle, 2, val, 4)
If ret1 = 0 Then
Dim m As New Margins(0, 0, 0, 0)
Dim ret2 As Integer = DwmExtendFrameIntoClientArea(Me.Handle, m)
Return ret2 = 0
Else
Return False
End If
Catch ex As Exception
' Probably dwmapi.dll not found (incompatible OS)
Return False
End Try
End Function
Protected Overrides Sub OnHandleCreated(e As EventArgs)
CreateDropShadow()
MyBase.OnHandleCreated(e)
End Sub
The result of above code creates a nice drop shadow effect on my borderless winform, but it causes the UI to distort. All the controls and labels on my form aren't appearing properly, with text not readable.
Am I missing something here? I don't want to use the traditional drop shadow effect using CreateParams, its too 'boxy' look and doesn't give a nice shadow effect.
Here are screenshots of without shadow and with shadow:
Thanks.
Dim m As New Margins(0, 0, 0, 0)
There's a subtle mistake visible here, looks a lot like you are using System.Drawing.Printing.Margins. But that's not a type that's compatible with the Windows' MARGINS type. Which is a structure, not a class.
So this just goes complete wrong, Windows reads nonsense instead of (0, 0, 0, 0). And extends the frame into the entire client area. Which then plays havoc on any control that draws with GDI, it is a 24bpp drawing api that leaves the alpha at 0 so anything that should be black becomes transparent instead.
Fix this by declaring a proper MARGINS structure:
Structure MARGINS
Public Left, Right, Top, Bottom As Integer
End Structure

Create Image from Graphics

In VB.NET, I need to create an Image based on a Graphics object I have. However, there is no method such as Image.fromGraphics() etc. What should I do then?
Try something like this MSDN article states. Essentialy create a Graphics Object from a Bitmap. Then use Graphic methods to do what you need to to the Image and then you can use the Image how you need to. As #Damien_The_Unbeliever stated your Graphics Object is created to enable drawing on another object, it does not have an Image to copy, the object it was created on does.
From above article:
Dim flag As New Bitmap(200, 100)
Dim flagGraphics As Graphics = Graphics.FromImage(flag)
Dim red As Integer = 0
Dim white As Integer = 11
While white <= 100
flagGraphics.FillRectangle(Brushes.Red, 0, red, 200, 10)
flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10)
red += 20
white += 20
End While
pictureBox1.Image = flag
Have a look at the Graphics.DrawImage method and its overloads.
Here's a snippet from one of the examples that draws an image onto the screen, using a Graphics object from Winform's Paint event:
Private Sub DrawImageRect(ByVal e As PaintEventArgs)
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create rectangle for displaying image.
Dim destRect As New Rectangle(100, 100, 450, 150)
' Draw image to screen.
e.Graphics.DrawImage(newImage, destRect)
End Sub

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