Center drawn text - vb.net

I'm drawing text in VB.net by using:
gfx.DrawString(_bText, New Font("Tahoma", 5), Brushes.Black, New Point(25, 5))
where gfx is a graphics object using my control. The x point is correct but I need the y to be the center of the current contol (vertically). Is there an easy way to do this?

You need to look at the Graphics.MeasureString method
Using this you can find the Height of your text in the context you give it. You then need to find the Y value to start drawing your text using something like this:
(ControlHeight/2) - (TextHeight/2)

Use the DrawString overload that takes a StringFormat argument. Set its Alignment property to Center.

TextRenderer has a VerticalCenter flag:
Dim r As New Rectangle(25, 0, myControl.ClientSize.Width - 25, _
myControl.ClientSize.Height)
Using myFont As New Font("Tahoma", 5)
TextRenderer.DrawText(gfx, _bText, myFont, r, _
Color.Black, Color.Empty, _
TextFormatFlags.VerticalCenter)
End Using

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

Capture Snapshot image from video in AxWindowsMediaPlayer in 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.

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

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.

Get Font Width within VB.net Without Measuring a Control?

I need to display a table in rich text within a form window. It is only a two column table, so tabbing works fine to line everything up (since RichTextBox does not support RTF tables). However, sometimes the tab stops are incorrect because of non-fixed width fonts.
So, I need a way to measure the pixel width of a particular string with a particular font (Arial 10) and space or tab pad to make sure that everything aligns.
I know about the Graphics.MeaureString method, but since this is in a rich text box, I don't have an initilzed PaintEventArgs variable, and that would seem like overkill to create JUST to measure one string.
From MSDN:
Private Sub MeasureStringMin(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
So is the best bet just to create a dummy PaintEventArgs instance? If so, what is the best way to do that (since I'll have to call this string measuring method several hundred times)?
Also, I don't really want to have to use a fixed width font - they just don't look as good.
Use this
Dim g as Graphics = richbox.CreateGraphics()
Dim sz as SizeF = g.MeasureString(...)