Image zoom WITH pixelation - vb.net

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

Related

vb.net draw brush picturebox location uncorrect place of mouse cursor

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

Draw Rectangle over PictureBox

The next code lets you draw Rectangles in the Form with mouse clics.
Why not, or how can be draw over a PictureBox?
Public Class Form1
Dim SelectRect As Rectangle = New Rectangle()
Dim ps As Point = New Point()
Dim pe As Point = New Point()
This catch the first click, starting point or corner of the rectangle
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
SelectRect.Width = 0
SelectRect.Height = 0
SelectRect.X = e.X
SelectRect.Y = e.Y
ps.X = e.X
ps.Y = e.Y
pe = ps
End Sub
This part determine the width and height of the rectangle:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
End If
End Sub
This part determine the last coordinate, the second corner of the rectangle:
Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim g As Graphics = Me.CreateGraphics()
Dim p As Pen = New Pen(Color.Blue, 2)
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
g.DrawRectangle(p, SelectRect)
g.Dispose()
End Sub
End Class
Your code uses a control (a Form in this case) mouse events to enable the drawing of rectangular shapes, with the help of guidelines, provided by Control.DrawReversibleFrame().
You just have to define the same events of a different, drawable, control - like a PictureBox - and repeat, more or less, the same procedure (after a cleanup).
As many have stated, here and before, use the Graphics object that
the Paint event kindly offers, so that your drawing will persist.
The Graphics object you get from Control.CreateGraphics() is not
persistent, and it can be erase/clipped when you don't want to.
Use it only if that is really what you have planned to do for the
reasons you know.
I've adden an event handler that checks if Control Key is pressed.
If Control is pressed, you add a rectangle, if not, only one rectangle is drawn.
I've also included, as an example, a line of code that fills the rectangle. I think it's interesting, because you have to control the size of the invalidated Region.
Comment out these lines of code to draw just the frame:
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
Dim SelectRect As Rectangle = New Rectangle()
Dim _pen As Pen = New Pen(Color.Green, 4)
Dim _brush As SolidBrush = New SolidBrush(Color.Orange)
Dim _ControlPressed As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
SelectRect.Location = e.Location
SelectRect.Size = New Size(0, 0)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If (e.Y < SelectRect.Y) Then
SelectRect.Location = If(SelectRect.Location.X > e.X,
New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
Else
If SelectRect.Location.X > SelectRect.Right Then
SelectRect.Location = New Point(e.X, SelectRect.Y)
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
End If
End If
If _ControlPressed Then
Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
_InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
PictureBox1.Invalidate(_InflatedRect)
Else
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
'Draw the outer rectangle with the color of _pen
e.Graphics.DrawRectangle(_pen, SelectRect)
'Fill the rectangle with the color of _brush
'It's half Pen.Width smaller so it doesn't erase the contour
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
End Sub

How to print the picture of any size to full A4 page in VB NET?

I have image of any size (eg 100x100 pixels) and I need to print stretched to the fixed size A4 (210 x 297 MM - mm, not pixels..)
How to print the picture of any size to full A4 page in VB NET?
Sample code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim newMargins As System.Drawing.Printing.Margins
newMargins = New System.Drawing.Printing.Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.Margins = newMargins
e.Graphics.DrawImage(picSource.Image, 0, 0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PrintDocument1.Print()
End Sub
Create a Rectangle and draw the image to that with this Overload for the DrawImage method. Change the x,y and size as needed.
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim newMargins As System.Drawing.Printing.Margins
newMargins = New System.Drawing.Printing.Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.Margins = newMargins
Dim ImgRect As New Rectangle(0,0,100,100)
e.Graphics.DrawImage(picSource.Image, ImgRect)
End Sub

Printing Image, Text and Margins?

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

About Drawing a moveable rectangle of an uploaded image in Visual Basic

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.