-
Sep 4th, 2024, 01:42 AM
#1
Thread Starter
New Member
Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Hello
Will it be possible to rotate an image from 0 to 360 in Zoom mode ? If No then there is no point in continuing further more.
if Yes and with some possibilities then below basic coding from my end
Have two images and their original sizes are as follows
Img A. ) Size is 800 W and 779 H
For ImgA Coding
Code:
Public Class Frm
Private imgPathA As String = "C:\Images\imgA.jpg"
Private Sub Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
PictureBox1.Size = New Size(800, 700)
PictureBox1.Image = Image.FromFile(imgPathA)
End Sub
ImgA is rotating as per below _PaintEvent
Code:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += txtBxDegrees.Text
PictureBox1.Invalidate()
PictureBox1.Image = Image.FromFile(imgPathA)
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
Dim imgBmp = New Bitmap(imgPathA)
.DrawImage(imgBmp, -imgBmp.Width \ 2, -imgBmp.Height \ 2)
End With
End Sub
End Class
Original size for below
Img B. ) size is 500 W and 500 H
ImgB Coding
Code:
Public Class Frm
Private imgPathB As String = "C:\Images\imgB.jpg"
Private Sub Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
PictureBox1.Size = New Size(800, 700)
PictureBox1.Image = Image.FromFile(imgPathB)
End Sub
ImgB is Not Rotating as per below _PaintEvent
Code:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += txtBxDegrees.Text
PictureBox1.Invalidate()
PictureBox1.Image = Image.FromFile(imgPathB)
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
Dim imgBmp = New Bitmap(imgPathB)
.DrawImage(imgBmp, -imgBmp.Width \ 2, -imgBmp.Height \ 2)
End With
End Sub
End Class
Is there any possiblity for Zoomed imageB to rotate 0 to 360 degrees ?
Thanks
nkVb
Last edited by nkvb; Sep 4th, 2024 at 01:46 AM.
-
Sep 4th, 2024, 11:31 AM
#2
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
If it works for one image, it will work for the other, so if it is not, then there's something else going on here. I see a few oddities. For one thing, the call to Invalidate probably does nothing. It doesn't help, but it probably doesn't do anything much at all. After all, it would just cause the image to become 'dirty' such that it paints...but it's going to do that anyways, because you're changing the image (as far as the control knows), so it will be dirty anyways. I don't think this will matter, but it could have a negative impact depending on exactly what happens.
The other point that I trip over is that you create a new bitmap every time through the Paint event. That should happen a LOT, and that can't be good. In fact, you appear to be doing it twice, because you call Image.FromFile, then later create a new Bitmap. Both of those create new Images, and do so in every Paint event, and Paint events happen in response to all kinds of things, including debugging. One way or another, I would guess that this is the cause. I'm actually a bit surprised that the first one does work, as it feels like you're rotating the wrong thing, but then again, I haven't rotated images and don't know the quirks around it.
My usual boring signature: Nothing
-
Sep 4th, 2024, 10:45 PM
#3
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
SH Sir,
Thank you so much for the response
the call to Invalidate probably does nothing. It doesn't help, but it probably doesn't do anything much at all. After all, it would just cause the image to become 'dirty' such that it paints...but it's going to do that anyways, because you're changing the image (as far as the control knows), so it will be dirty anyways. I don't think this will matter, but it could have a negative impact depending on exactly what happens
If I put ' mark before PictureBox1.Invalidate(). The imageA does not rotate which was rotating. FYI
Probably PictureBox1.Invalidate() is required for rotating the image.
In fact, you appear to be doing it twice, because you call Image.FromFile, then later create a new Bitmap. Both of those create new Images, and do so in every Paint event, and Paint events happen in response to all kinds of things, including debugging. One way or another, I would guess that this is the cause. I'm actually a bit surprised that the first one does work
Informative. Let me check on this and revert back
nkVb
-
Sep 5th, 2024, 12:21 AM
#4
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Dear SH sir,
because you call Image.FromFile
Have removed the full command 'PictureBox1.Image = Image.FromFile(imgPathA) from both the examples as mentioned above
Also emphasized on your remark
After all, it would just cause the image to become 'dirty'
which made me think practically to change and increase the size of imgB from 500 W X 500 H to 730 W and 730 H which I did through https://imageresizer.com/. Also changed the Picturebox1.Size to suit the desired requirements.
Under the circumstances, Though not satisfied with sharpness of image, I am moving on.
Thank you so much Sir, for the information that you have provided here.
nkVb
-
Sep 5th, 2024, 05:42 PM
#5
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Indeed, it is possible to turn an image from 0 to 360 degrees in the Zoom mode. The problem is that you’re using the SizeMode = PictureBoxSizeMode.Zoom property which fits the image to the PictureBox then the rotation is applied. To rectify this draw the image by loading it into a Bitmap, apply the rotation transformation on the Bitmap then draw the new Bitmap on the PictureBox.
-
Sep 5th, 2024, 09:58 PM
#6
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Originally Posted by build
…then draw the new Bitmap on the PictureBox.
Set the .Image property of the PictureBox. DON’T draw on the PictureBox.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 6th, 2024, 02:34 AM
#7
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
I thought I resolved the issue from very different perspective but it seems there is coding to this.
build
Indeed, it is possible to turn an image from 0 to 360 degrees in the Zoom mode. The problem is that you’re using the SizeMode = PictureBoxSizeMode.Zoom property which fits the image to the PictureBox then the rotation is applied. To rectify this draw the image by loading it into a Bitmap, apply the rotation transformation on the Bitmap then draw the new Bitmap on the PictureBox.
.Paul
Set the .Image property of the PictureBox. DON’T draw on the PictureBox
Can you please elaborate it. I am confused as I am working with Images, Picturebox , .DrawImage etc for first time. I tried to adopt your meaning to the below coding but somewhere I've made blunder.
What i've understood I've removed the PictureBox1.sizeMode = PictureSizeMode.Zoom
below my Paint event is completely wrong after going though your replies and implementing the same and against posted in #1
Code:
Public Class Frm
Private imgPathB As String = "C:\Images\imgB.jpg"
Private Sub Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
PictureBox1.Size = New Size(800, 700)
PictureBox1.Image = Image.FromFile(imgPathB)
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += txtBxDegrees.Text
PictureBox1.Invalidate()
'PictureBox1.Image = Image.FromFile(imgPathB)
'PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
With e.Graphics
.DrawImage(Image.FromFile(imgPathB), New Point(100, 5))
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
'PictureBox1.Image = imgBmp 'Image.FromFile(imgPathB)
End With
End Sub
Thanks
nkVb
Last edited by nkvb; Sep 6th, 2024 at 02:40 AM.
-
Sep 6th, 2024, 04:01 AM
#8
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
This code loads a bitmap in a picturebox…
Code:
PictureBox1.Image = Image.FromFile(imgPathB)
This code draws on a picturebox…
Code:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += txtBxDegrees.Text
‘ text is a String. It is not a double
PictureBox1.Invalidate()
With e.Graphics
.DrawImage(Image.FromFile(imgPathB), New Point(100, 5))
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
End With
End Sub
This code draws in a bitmap, which you then load into your picturebox image…
(Don’t put this code in your PictureBox_Paint event handler sub)
Code:
Dim img As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim gr As Graphics = Graphics.FromImage(img)
With gr
.TranslateTransform(img.Width \ 2, img.Height \ 2)
.RotateTransform(CSng(dgr))
.DrawImage(Image.FromFile(imgPathB), New Point(100, 5))
End With
PictureBox1.Image = img
Edit: The first two code snippets are just to show you the differences between loading an image in a picturebox, and alternatively drawing on a picturebox.
The third code snippet shows you the best way to achieve what you’re trying to achieve…
Last edited by .paul.; Yesterday at 12:42 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
.Paul
Indeed Good Explanation with 3 Code snippets of respective differentation
The third code snippet shows you the best way to achieve what you’re trying to achieve…
This code draws in a bitmap, which you then load into your picturebox image…
(Don’t put this code in your PictureBox_Paint event handler sub)
I incorporated your code in formlayout
Code:
Private Sub FormLayout()
PictureBox1.Size = New Size(800, 700)
Dim img As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim gr As Graphics = Graphics.FromImage(img)
With gr
'.TranslateTransform(img.Width \ 2, img.Height \ 2)
'.RotateTransform(CSng(dgr))
.DrawImage(Image.FromFile(imgPathB), New Point(100, 5))
End With
PictureBox1.Image = img
End Sub
I've temporarily removed the .TranslateTransform(img.Width \ 2, img.Height) \ & .RotateTransform(CSng(dgr))
How do I zoom the above image ? because PictureBox1.SizeMode = PictureBoxSizeMode.Zoom has no effect
How to rotate the same using below Paint Event because the image shifts from original location to center, towards right side, below,
right down corner of PictureBox etc... and not rotating from center
Code:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += Val(txtBxDegrees.Text)
PictureBox1.Invalidate()
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
Dim img As New Bitmap(PictureBox1.Width, PictureBox1.Height)
.DrawImage(Image.FromFile(imgPathB), -img.Width \ 2, -img.Height \ 2)
End With
End Sub
Wanting to Zoom Image and rotate the same
Thanks nkvb
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
I’ve told you how the picturebox works. If you’re drawing on the picturebox, as you are, the zoom is going to have absolutely no effect. Zoom only works on images in the picturebox, not painted on the picturebox.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
If your bitmap is the same size as the picturebox you’re using to display the bitmap, your pictureboxSizeMode.zoom won’t stretch or shrink your image, because it already fits your picturebox. I really don’t know what you expect to happen…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
This is a simple example of rotating a square in a PictureBox. I used a NumericUpDown control instead of a TextBox...
Code:
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Dim img As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim gr As Graphics = Graphics.FromImage(img)
With gr
.TranslateTransform(img.Width \ 2, img.Height \ 2)
.RotateTransform(CSng(NumericUpDown1.Value))
.FillRectangle(Brushes.Red, New Rectangle(-150, -150, 300, 300))
End With
PictureBox1.Image = img
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Thank you .Paul for your comments in #10, #11 & #12. this made the concept quite clear of Picture box and Images.
I've not tried your solution as per #13 but definately will give it a try.
I had almost given up but what i could achieve from StackOverflow Link https://stackoverflow.com/questions/2144592/resizing-images-in-vb-net
and my re-coding goes as below
Code:
Public Class Frm
Private imgPathB As String = "C:\Images\imgB.jpg"
Private Sub Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
PictureBox1.Size = New Size(800, 700)
PictureBox1.Image = Image.FromFile(imgPathB)
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim dgr As Double
If dgr = 360 Then dgr = 0
dgr += txtBxDegrees.Text
PictureBox1.Invalidate()
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(CSng(dgr))
Dim imgBmp = New Bitmap(imgPathB)
imgBmp = ResizeImage(imgBmp, PictureBox1.Width, PictureBox1.Height)
.DrawImage(imgBmp, -imgBmp.Width \ 2, -imgBmp.Height \ 2)
'the above rotates
End With
End Sub
Public Shared Function ResizeImage(ByVal InputBitmap As Bitmap, width As Integer, height As Integer) As Bitmap
Return New Bitmap(InputBitmap, New Size(width, height))
End Function
With the above one can re-size the image as being represented as Zoomed Image and happy to rotate it.
Thanks
nkvb
-
Thread Starter
New Member
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
.Paul
Have tried your coding as per #13 and beautiful Red Square rotating.
and if I click down in NumericUpDown ie below 0. it does not go in negative value and to rotate the image in reverse direction.
Thanks
nkvb
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
Originally Posted by nkvb
.Paul
…and if I click down in NumericUpDown ie below 0. it does not go in negative value and to rotate the image in reverse direction.
Thanks
nkvb
Is that a good thing or a bad thing?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Re: Rotating an Image from 0 to 360 degrees in Zoom Mode. Any possibilities ?
The NumericUpDown has Minimum, Maximum, and increment Properties that you might find useful
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|