How to create an overlay of a full screen application - vb.net

So I'm trying to make an overlay for a full screen game. Firstly this isn't for hacking purposes, the game shows times as little clocks and I find this difficult to read, so when I press a key to activate something I want to be able to see how long ago I activated it etc.
I currently have this code to write the time to the screen:
Private Shared Sub DrawOverlay(ByVal OverlayString As String)
Dim Processes = Process.GetProcessesByName("GAMECLIENT")
Dim deviceContext As Graphics = Graphics.FromHwnd(Processes(0).MainWindowHandle)
Dim drawFont As New Font("Arial", 14, FontStyle.Bold)
Dim myBrush As New Drawing2D.LinearGradientBrush(deviceContext.ClipBounds, Color.Black, Color.SkyBlue, Drawing2D.LinearGradientMode.Horizontal)
Dim drawFormat As New StringFormat()
drawFormat.FormatFlags = StringFormatFlags.NoFontFallback
deviceContext.DrawString(OverlayString, drawFont, myBrush, 100, 100, drawFormat)
End Sub
To be clear this code isn't mine. It's just what I've found from research. This works, however it's extremely slow. It actually makes the game free for about 5 seconds. I think the time consuming line is:
deviceContext.DrawString(OverlayString, drawFont, myBrush, 100, 100, drawFormat)
By process of playing around with other lines it seems to be almost defiantly this line. However I can't work out how to speed it up. I even published the application, just in case it was the debugger. No difference. However this doesn't work for full screen which isn't ideal, if anyone could explain why that would probably help me out.
On a related note if anyone has/knows of a DLL I can reference to do this quickly and more effectively (ESPECALLY FOR FULL SCREEN) that would be great!
So to sum up:
1) Why is that line so slow
2) How should I speed it up
3) Is there another way to do this that works with full screen.
Thanks!

Related

Improve Performance With GDI+

As a challenge, I am planning to make a game purely in Visual Studio (using Visual Basic) but in the past, I have noted that GDI+ is not the best rendering tool, but for what it is, it's all right.
What settings should I use and not use when working with GDI+? Here are my assumptions:
InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor It would make sense that this option renders the fastest.
PixelOffsetMode = Drawing2D.PixelOffsetMode.Half This seems like it must be used with NearestNeighbor.
CompositingQuality = ? I am not sure about this one.
CompositingMode = ? Again, I can't tell.
SmoothingMode = Drawing2D.SmoothingMode.None Specifying no smoothing mode would probably run the fastest.
SetClip(ClientRectangle) It would make sense to set this as the clip so the graphics would not draw outside of the form or control.
My next point: I've noticed that adding all the graphics code always works the fastest in Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Is there any benefit to doing something like this inside that void: (Creating a temporary bitmap and drawing it onto the screen.)
Using B As New Bitmap(ClientSize.Width, ClientSize.Height, Imaging.PixelFormat.Format32bppPArgb)
Using G As Graphics = Graphics.FromImage(B)
' Draw objects onto the bitmap.
End Using
e.Graphics.DrawImage(B, 0, 0)
End Using
I ask this because I noticed that when creating a bitmap, you can specify a Imaging.PixelFormat. Is there one type of format that renders faster than the rest?
Thank you all in advance for the help.
~Nic

No result using BITMAP

Here's a code snippet:
Dim BMf As Bitmap
BMf = New Bitmap(PicBox.Width, PicBox.Height)
Call FourierPlot(PlotHeight, PlotWidth, RPMArray, CoefArray, Count, 2, BMf)
Me.PicBox.Image = BMf
BMf.Dispose()
within the subroutine is code such as:
Dim myGraphics As Graphics = Graphics.FromImage(BMf)
Dim myPen As New Pen(colr)
....
myGraphics.DrawLine(myPen, lastx, lasty, temp1, temp2)
....
myPen.Dispose()
myGraphics.Dispose()
The displayed PicBox consists of a boundary rectangle with lines draw from opposing corners (what I presume is an "error image.)
Any suggestions (hopefully VERY simple ones) as to what I'm doing wrong?
You shouldn't call BMf.Dispose(), because the image is going to be used by the picture box.
Update:
OK, small update because this seems to be confusing to the OP and others.
When you say Me.PicBox.Image = BMf you set a reference to the bitmap. Both BMf and PicBox.Image now point to the same object. Thus, you should not dispose it, because then it can't be used anymore. Again - this is not a copy, this is same object.
And about leaks. Removing this line does not cause any memory or other leaks. .NET is managing this for you. As soon as the bitmap will not be needed (which means nobody has a reference to it = your picture box will load another image), it will be unneeded and disposed at the next run of the garbage collector. This is automatic.
You might want to disposed it manually if you really need to care about resource, for example if you would invoked your code and create new bitmaps often, many times. But in this case you probably would be better off forcing garbage collector to run in places, where you can cause some delay to the user. You can do this like this:
GC.Collect()

Using GetWindowDC for drawing

I'm having difficulties in using Win API GetWindowDC to get a valid hDC for drawing.
I'm using vb.net 2013, and I tried to bitblt images to a form, draw string, draw rectangle, and nothing.
I'm running in Win7 all the tests.
Do I have to disable aero with DWM functions, is it a handle trick, or what is? I already posted a "draw in titlebar vb.net 2013" question that had no answer, where I have some code used, but right now, I just need a working example to start. All help is appreciated.
But here goes some more code that doesn't work either.
Select Case m.Msg
Case WinAPI.Win32Messages.WM_NCPAINT
Dim hDC As IntPtr = WinAPI.GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHwnd(hDC)
g.DrawString("TESTE", New Font("ARIAL", 16), New SolidBrush(Color.Black), 5, 5)
g.Dispose()
WinAPI.ReleaseDC(m.HWnd, hDC)
The error is OutOfMemory, and nothing gets drawn anywhere, even with tests done to clientarea.
It's a simple purpose. Getting to draw in all the areas of a form, and from that on, I'll handle the drawing.
Well, thanks at least for the help on correctly editing. About the Graphics.FromHDC, yes, I tried it.
But here goes some more code that doesn't work either.
Select Case m.Msg
Case WinAPI.Win32Messages.WM_NCPAINT
Dim hDC As IntPtr = WinAPI.GetWindowDC(m.HWnd)
Dim g As Graphics = Graphics.FromHwnd(hDC)
g.DrawString("TESTE", New Font("ARIAL", 16), New SolidBrush(Color.Black), 5, 5)
g.Dispose()
WinAPI.ReleaseDC(m.HWnd, hDC)
The error is OutOfMemory, and nothing gets drawn anywhere, even with tests done to clientarea.
It's a simple purpose. Getting to draw in all the areas of a form, and from that on, I'll handle the drawing. And too bad for the negative votes. Doesn't help, cause if people where fast on the negative voting...try to be as fast on reading all of what's placed in a question, and finding out that it is not just "gimme gimme codez" - the other referenced question has code.
People that know a lot forget that once you where like me, and didn't know a lot about the subject...might not even know how to ask the right questions...
Thanks for the help, if someone can provide it.
Graphics.FromHwnd() takes a window handle, not a DC (the clue is in the name). Try Graphics.FromHdc() instead.

set location of form instance

I am working on this.. the locations of the marker is in the database..
this is not google's API.
this is GMap.Net for windows.
this is the code..
For Each dtrow In markerDtable.Rows
Dim marker As New GMapMarkerGoogleGreen(New PointLatLng(dtrow("Latitude"), dtrow("Longitude")))
markersOverlay.Markers.Add(marker)
Next
I don't really understand so much, just experimented on code to get their supposed to be latlng and magically it appeared how I wanted it.
how can I make that look like this.. (this is only an emulation, I just dragged the form to what place I want them to be)
only form2 and I only need its instances.. when I click the marker, form2 appears right beside it.
UPDATE: I have my coordinates, but how can I make them appear like that? from my late versions, I use this.. form2.Location = marker.LocalPosition + New Point(20, -240) - to offset
but that code is pre-defined, hard coded. and that's not my goal. I want it on a sub, just like the code above (for markers) thank you for helping.
UPDATE2: current code - shows the three forms, but does not go to the location of the markers
For Each dtrow In markerDtable.Rows
Dim marker As New GMapMarkerGoogleGreen(New PointLatLng(dtrow("Latitude"), dtrow("Longitude")))
markersOverlay.Markers.Add(marker)
Dim f As New Form2
f.Location = marker.LocalPosition
f.Show()
Next

OutofMemory Exception on new Bitmap()

I have to draw something on a Image wich is captured by the Camera. This works on many Devices, but sometimes the RAM is too small or the pictures too big and the function crashed with a OutOfMemory Exception.
How am I able to:
a) optimize the code to prevent this Exceptions
b) Handle this Exceptions (making pictures smaller, free Ram etc.
here is the code:
Dim from_bmp As Bitmap
Dim bmp As Bitmap
from_bmp = New Bitmap(picname)
'I also tryed this with a function which is creating the Bitmap from Filestream
'I also tryed this with the OpenNETCF.Drawing.Imaging.IImage
'If the Original Pictiure is too big, the function will crash here.
bmp = New Bitmap(from_bmp.Width, from_bmp.Height + stampheight)
'now I Create a bitmap which is higher than the original, in order to write the stamp beneth the picture
Dim font As New System.Drawing.Font("Arial", 30, FontStyle.Regular)
gr = Graphics.FromImage(bmp)
gr.DrawImage(from_bmp, 0, 0)
from_bmp.Dispose()
'here I draw somethin in the Bitmap
bmp.Save(deststring, System.Drawing.Imaging.ImageFormat.Jpeg)
gr.Dispose()
bmp.Dispose()
I'd likely use a "using" pattern for your Bitmaps. Also, be aware that an OOM on Bitmap creation can often be overcome by simply trying again (here's a diatribe as to why). My bet is that the GC Heap is too full and a second request, especially after a collection, would succeed.
I'd go through the rest of the code (that not shown) and make sure that all other graphics objects are getting properly Disposed - the CF doesn't handle auto clean-up of the native resources of graphic objects very well and often needs a bit of help (again, see the above link).