I am loading and image onto a picturebox and drawing som text.
when i drag the text it looks big but then when i dra it or lock it in place it becomes too small. the picture box sizemode is set to Zoom. so huge images are made small. how can i set proper fontsize and preview while in picturebox?
Sub btnLock is responsible for "locking" Text to image
and
sub Picturebox1_Paint previews
how can i make preview scale down font size or make btnLock scaleup fontSIze
Dim Loc As Point
Dim Pxy As Point
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
txtButton.Text = MouseButtons.Left
If e.Button = MouseButtons.Left Then
Loc = e.Location
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim myFontSize As Integer
Integer.TryParse(ComboBox1.Text, myFontSize)
Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic)
Dim pt As Point = PictureBox1.PointToClient(Control.MousePosition)
e.Graphics.DrawString(txtA.Text, myFont, Brushes.Black, pt.X, pt.Y)
End Sub
Private Sub btnLock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLock.Click
Dim myFontSize As Integer
Integer.TryParse(ComboBox1.Text, myFontSize)
Dim myFont As New Font("Arial", myFontSize, FontStyle.Bold Or FontStyle.Italic)
Dim g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
' g.DrawString(txtA.Text, myFont, Brushes.Black, Loc.X, Loc.Y)
g.DrawString(txtA.Text, myFont, Brushes.Black, Pxy.X, Pxy.Y)
' Me.PictureBox1.Invalidate()
End Sub
Thank you for your help!-DB35M
I would think you are getting this behaviour as the two Graphics objects have different sizes. You object g is getting its bounds from the source image of your picturebox (without scaling) and your graphics object during PictureBox1 is getting it bounds from the picturebox container. This means they will be different sizes and therefore the myFontSize will look like it has different sizes.
Trying to "scale down" the font size according to the down-scaling of the image would be really hard to accomplish. Instead, solve the problem by changing your SizeMode to Normal and resizing the container to fit the image:
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
Related
I draw brush in picturebox but the location of the drawing appears away from the place of mouse cursor
and it always appear on top of the picture
Note This happen when sizemode in picture is set to StretchImage
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
Dim x, y As Integer
x = e.Location.X
y = e.Location.Y
Label1.Text = x.ToString
Label2.Text = y.ToString
Width = 2
If x = 0 Then
x = y
End If
Dim brush As SolidBrush = New SolidBrush(color)
Dim pen1 As Pen = New Pen(color, 4)
g.DrawRectangle(pen1, e.X, e.Y, 10, 10)
g.FillRectangle(brush, e.X, e.Y, 10, 10)
End Using
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
end Sub
When using PictureBoxSizeMode.StretchImage, that only affects what you see on the screen. The Image object doesn't change. Let's say that you have an Image that is 10x10 and a PictureBox that is 100x100. If you put the mouse pointer in the centre of the control then the location will be (50, 50). If the Image is only 10x10, how could (50, 50) possibly be used for a point in the Image? You would need to do some arithmetic to scale your mouse coordinates based on the scaling of the Image. If the Image is stretched by N times in a direction then you have to divide the mouse coordinate by N in that direction to map to the corresponding point on the actual Image:
finalValue = imageDimension * initialValue / controlDimension
That's going to give you a Double value, so you need to decide what you want to do with that. You might convert it to a Single and then use the overloads of DrawRectangle and FillRectangle that accept Single values.
I have to use the stretched image after being stretched
using PictureBox1.CreateGraphics.DrawRectangle
Private draw As Boolean = False
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
draw = True
End Sub
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Label1.Text = CStr(e.X)
Label2.Text = CStr(e.Y)
PictureBox1.CreateGraphics.DrawRectangle(Pens.Beige, e.X, e.Y, 10, 10)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
draw = False
End Sub
I spent few days trying and looking for Vb.net script that can combine the three (Printing Image + Printing Text + changing Margins); but no luck;
I want to:
Print image in PictureBox1;
Print text from RichTextBox1 (below OR above the image);
Adjust the page margins;
Also, I appreciate it if I can get help to Print-Preview the above before printing;
Below is my start up script that will only manipulate the text:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
printDocument1.Print()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.ShowDialog()
End Sub
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
Dim fnt_t As Font
fnt_t = RichTextBox1.Font
e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.Black), 10, 10)
End Sub
End Class
This answer is based on your comment on wanting to draw text on your image.
When you are drawing items to the PrintDocument the one that is drawn first will be underneath any item that is drawn later, if they are occupying the same location on the Graphic surface. This is an example on how to center text in the Image.
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim fnt_t As Font = RichTextBox1.Font 'Set your font
Dim rect As Rectangle = New Rectangle(New Point(0, 0), PictureBox1.Image.Size) ' used to give a common reference
Dim fmt As StringFormat = New StringFormat()
fmt.Alignment = StringAlignment.Center 'Horizontal Centering
fmt.LineAlignment = StringAlignment.Center 'Vertical Centering
e.Graphics.DrawImage(PictureBox1.Image, rect) 'Draw Image
e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.White), rect, fmt) 'Draw Text
End Sub
And this example is how to add text to the bottom of the image.
Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim fnt_t As Font = RichTextBox1.Font 'Set your font
Dim rect As Rectangle = New Rectangle(New Point(0, 0), PictureBox1.Image.Size) ' get the size of the image
Dim lineheight As Integer = TextRenderer.MeasureText(RichTextBox1.Text, fnt_t).Height ' Measure to find height of text
Dim stringRect As Rectangle = New Rectangle(0, rect.Bottom - lineheight, rect.Width, lineheight) 'Determine rectangle for Text
Dim fmt As StringFormat = New StringFormat() 'Tell it to center Text in its rectangle
fmt.Alignment = StringAlignment.Center 'Center it
e.Graphics.DrawImage(PictureBox1.Image, rect) 'Draw Image
e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.White), stringRect, fmt) 'Draw Text
End Sub
I am zooming image in picture box using this code on trackbar.
Private Sub tbrZoomLevel_Scroll(sender As System.Object, e As System.EventArgs) Handles tbrZoomLevel.Scroll
With pbImage
.SuspendLayout()
.Width = actualSize.Width * tbrZoomLevel.Value
.Height = actualSize.Height * tbrZoomLevel.Value
.ResumeLayout()
End With
End Sub
pbImage is a PictureBox control with sizemode as zoom.
actualSize is original Size of Image in pbImage.
When I zoom in, I get image without pixelation. But I want it to get fully pixelated and show the image as shown in MS Paint on zooming. Any help is appreciated. VB.Net code, C# code or just any algorithm is welcomed.
In your picturebox paint:
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
'Draw the image
End Sub
Edit: Try this
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim srcRect As RectangleF = New Rectangle(0, 0, PictureBox1.Width / 8, PictureBox1.Height / 8)
Dim dstRect As RectangleF = New RectangleF(0, 0, PictureBox1.Width, PictureBox1.Height)
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(pctBoxImage, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub
pctBoxImage is the bitmap which will be 800% zoomed
or
Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.ScaleTransform(8, 8)
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.DrawImage(pctBoxImage, 0, 0)
End Sub
0,0 is the coordinates of the left up corner position on the bitmap.
valter
I've been working on a moveable rectangle to compute the ROI of an Image in VB 2010, Since i'm new to VB, I've been able to create a rectangle, But it doesnot appear on my uploaded image. It appears but not on the image. By my listed code below
1. How do I get the rectangle to display on the image.
2. How do get the rectangle to become moveable through out the image. I will be grateful. thanks.
Private Sub ROIToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ROIToolStripMenuItem.Click
Dim G As Graphics
G = PictureBox1.CreateGraphics
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
G.FillRectangle(Brushes.Silver, ClientRectangle)
Dim P As Point
Dim Box As Rectangle
P.X = 1
P.Y = 1
Dim S As Size
S.Width = 100
S.Height = 20
Box = New Rectangle(P, S)
G.DrawRectangle(Pens.Red, Box)
Try the below code
Dim G As Graphics
Dim uploadedBmp1, backgroundBmp1 As Bitmap
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
uploadedBmp1 = New Bitmap("C:\Windows\winnt.bmp")
backgroundBmp1 = New Bitmap(uploadedBmp1.Width, uploadedBmp1.Height)
G = Graphics.FromImage(backgroundBmp1)
PictureBox1.Image = backgroundBmp1
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DrawRectangleOnImage(New Point(0, 0), New Size(100, 50))
End Sub
Private Sub DrawRectangleOnImage(ByVal point1 As Point, ByVal size1 As Size)
G.Clear(Color.White)
G.DrawImage(uploadedBmp1, 0, 0, uploadedBmp1.Width, uploadedBmp1.Height)
Dim rectangle1 As New Rectangle(point1, size1)
G.DrawRectangle(Pens.Red, rectangle1)
PictureBox1.Invalidate()
End Sub
to make a moving rectangle, just call the DrawRectangleOnImage sub with the parameters that you want. It will automatically clear the previous snapshot and draw everything again.
I am using Visual Basic.Net and am drawing graphics on the screen.
Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim gr As Graphics = Graphics.FromHwnd(New IntPtr(0))
gr.DrawString("text on screen",
New Font(Me.Font.FontFamily, 25,
FontStyle.Regular), Brushes.Red, 50, 50)
End Sub
In the above code, text is drawn on the screen. My question is this: How can I remove the text that is drawn to screen? I see that there is a .Clear method, however, this 'Clears the entire drawing surface and fills it with the specified background color', rather than just removing the drawn text.
Thanks in advance.
EDIT
I am wanting to develop a subliminal message application that will show messages on screen while the user is using other applications. Would the transparent form be the best way to do this?
I have found the following code that works:
Private WithEvents TextForm As New Form
Private Zipper As New FontFamily("Zipper")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With TextForm
.BackColor = Color.DimGray
.TransparencyKey = Color.DimGray
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.ShowInTaskbar = False
.WindowState = FormWindowState.Maximized
.Opacity = 0
.Show(Me)
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Static showText As Boolean
showText = Not showText
If showText Then TextForm.Opacity = 0.99 Else TextForm.Opacity = 0
End Sub
Private Sub TextForm_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles TextForm.Paint
e.Graphics.DrawString("text on screen 12345", New Font(Zipper, 30, FontStyle.Bold), Brushes.Red, 50, 50)
End Sub
Take different bitmaps and draw each new thing in separate bitmaps and after merge new bitmap with old one. When you want to remove text reload old bitmap which is without text.
Search for drawing in new bitmaps and save drawing.
You can try this:
Dim Graphics0 as Graphics = Graphics.fromHwnd(0) 'This is the desktop's graphics
Graphics0.DrawText("Test 1..2..3..",New Font(Arial,10),Brushes.Black,New Point(0,0))