vb.net在缩放图像上绘图 [英] vb.net drawing on zoomed image

查看:143
本文介绍了vb.net在缩放图像上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



所以我得到了这个项目,制作一个像应用程序一样的颜料。应用程序必须加载和映像,并且必须能够在该图像上绘制。



这会加载图像并进行编辑以进行编辑:

Quote:

PictureBox4.Image = Image.FromFile(str)

PictureBox4.SizeMode = PictureBoxSizeMode.Zoom

Me.WindowState = FormWindowState.Maximized

Drawbitmap =新位图(PictureBox4.Image)

Drawgraphics = Graphics.FromImage(Drawbitmap)

PictureBox4.Image = Drawbitmap

绘图=错误





这样可以在图片框上绘图:



Quote:

Private Sub PictureBox4_MouseDown(ByVal sender As System.Object,ByVal e As System .Windows.Forms.MouseEventArgs)处理PictureBox4.MouseDown



绘图=真



结束子




这会禁用图片框上的绘图:



引用:

Private Sub PictureBox4_MouseUp(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs)Handles PictureBox4.MouseUp



Drawing = False



End Sub





这就是图纸



Quote:

Private Sub PictureBox4_MouseMove(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs )处理PictureBox4.MouseMove

如果绘图然后

xStart = eX

yStart = eY

drawMyline()



结束如果

xEnd = eX

yEnd = eY



End Sub





这是drawMyine函数:



Quote:

Private Sub drawMyline()



PictureBox4.Image = Drawbitmap

Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

Drawgraphics.DrawLine(p,xStart,yStart,xEnd,yEnd)

p.StartCap = LineCap.Round

p.EndCap = LineCap.Round



End Sub





我的问题如下:一切都很好,除非我绘制,绘图不会从光标所在的位置开始,如果我改变

引用:

PictureBox4.SizeMode = PictureBoxSizeMode.Normal

绘图开始它应该(在光标位置)但是如果我使用

引用:

PictureBoxSizeMode.Normal

如果我正在加载它的图像大于picturebox4宽度并且高度看起来它看起来被裁剪。我可以使用

Quote:

PictureBoxSizeMode.Autosize

但是图片不应该有任何滚动条可见因此我必须使用缩放,因为它适合里面的图像图片框。有人可以帮我弄清楚如何在放大的图像上正确绘制?谢谢!



P.S.如果你有什么不明白的地方请告诉我。

解决方案

不要在 PictureBox上画任何东西。这是许多初学者常见的错误之一,这个错误被这个几乎无用的类所困惑,只是为了简化最基本的图形杂务,比如呈现静态图像。尽管可以使用 PictureBox 进行操作,但它绝对不会给你带来额外麻烦,浪费开发时间和额外资源。在所有更复杂的情况下,你应该忘记它就像一场噩梦,直接在一些自定义控件上渲染图形,甚至可以作为绘图板使用一些控件,例如Panel。



我会解释该怎么做。请查看我过去的答案:

在其中附加图片图片框 [ ^ ],

画一个矩形C# [ ^ ],

如何从旧图纸中清除面板 [ ^ ],

什么样的p Layful方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ](解释它是如何工作的),

mdi子窗体之间的绘制线 [ ^ ],

如何加快我的vb.net应用程序? [ ^ ],

用C#.net鼠标滚轮缩放图像 [ ^ ](这个特别是放大)。



-SA


我发现了如何做到这一点



这是我从中得到代码的网址

http://www.daniweb.com/software-development/vbnet/threads/151127/correctly-displaying-image-in-picturebox [ ^ ]



注意!!这将缩放图像以适应图片框,因此如果您决定保存图像,它将使用缩放位图的分辨率保存它(如果您将具有1920x1080的图像加载到图片框中,您将以不同的分辨率保存较低的图像因为它是缩放的)。要以相同的分辨率保存图像,请执行以下操作:



假设您要在form_load上加载图像,但在任何事情之前声明两个变量如下所示

Quote:

Dim defaultHeight As Integer

Dim defaultWidth As Integer





现在在form_load事件中添加这个



引用:

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load

Dim PicBoxHeight As Integer

Dim PicBoxWidth As整数

Dim ImageHeight As Integer

Dim ImageWidth As Integer

Dim TempImage As Image

Dim scale_factor As Single

PictureBox1.SizeMode = PictureBoxSizeMode.Normal

'获取控件大小

PicBoxHeight = PictureBox1.Height

PicBoxWidth = PictureBox1.Width

'加载图片。更改Image.FromFile()以匹配您的代码

TempImage = Image.FromFile(str)

defaultWidth = TempImage.Width'是所选图像的宽度(未缩放)

defaultHeight = TempImage.Height'是所选图像的高度(未缩放)

'获取图像大小

ImageHeight = TempImage.Height

ImageWidth = TempImage.Width

'计算scale_factor

scale_factor = 1.0'默认情况下没有缩放,即图像适合框中

'1)身高

如果ImageHeight> PicBoxHeight然后

'首先降低高度

scale_factor = CSng(PicBoxHeight / ImageHeight)

结束如果

'2 )宽度。请注意,我们现在知道我们有多少要缩小高度

'而scale_factor也会影响宽度

If(ImageWidth * scale_factor)> PicBoxWidth然后

'缩放宽度超过Box的宽度,重新计算scale_factor

scale_factor = CSng(PicBoxWidth / ImageWidth)

结束如果

'将图像移动到控件以调整大小

PictureBox1.Image = TempImage

'获取源位图。

Dim bm_source As New Bitmap (PictureBox1.Image)

'为结果创建一个位图。

Dim bm_dest作为新位图(_

CInt(bm_source.Width * scale_factor),_

CInt(bm_source.Height * scale_factor))

'为结果位图创建一个Graphics对象。

Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

'将源图像复制到目标位图。

gr_dest.DrawImage(bm_source,0,0,_

bm_dest.Width + 1,_

bm_dest.Height + 1)

'显示结果。

PictureBox1.Image = bm_dest

Drawbitmap =新位图(PictureBox1.Image)

PictureBox1.Height = Drawbitmap.Height

Drawgraphics = Graphics.FromImage(Drawbitmap)

End Sub





Picturebox1只是一个例子用你的图片框替换它

现在你单击我的情况下的保存按钮它是另一个图片框(你可以使用一个按钮)添加到click事件



Quote:

Private Sub PictureBox9_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理PictureBox9.Click

Dim bmp As New Bitmap(PictureBox1.Image,defaultWidth,defaultHeight)'这将从Picturebox1.image获取位图并将其缩放到它的原始宽度和hight

bmp.Save(C:\ image.jpg)

bmp.Dis pose()

MessageBox.Show(已保存!)

End Sub





我希望在阅读本文时不会有任何误解。再次感谢您的回复。


hello !

so i got this project , to make a paint like application . The Application must load and image and one must be able to draw on that image .

This loads the image and preps it for editing :

Quote:

PictureBox4.Image = Image.FromFile(str)
PictureBox4.SizeMode = PictureBoxSizeMode.Zoom
Me.WindowState = FormWindowState.Maximized
Drawbitmap = New Bitmap(PictureBox4.Image)
Drawgraphics = Graphics.FromImage(Drawbitmap)
PictureBox4.Image = Drawbitmap
Drawing = False



This enables drawing on the picturebox :

Quote:

Private Sub PictureBox4_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseDown

Drawing = True

End Sub



This disables the drawing on the picturebox :

Quote:

Private Sub PictureBox4_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseUp

Drawing = False

End Sub



And this does the drawing

Quote:

Private Sub PictureBox4_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseMove
If Drawing Then
xStart = e.X
yStart = e.Y
drawMyline()

End If
xEnd = e.X
yEnd = e.Y

End Sub



This is the drawMyine function :

Quote:

Private Sub drawMyline()

PictureBox4.Image = Drawbitmap
Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Drawgraphics.DrawLine(p, xStart, yStart, xEnd, yEnd)
p.StartCap = LineCap.Round
p.EndCap = LineCap.Round

End Sub



My problem is the following : Everything works great except when i draw , the drawing does not start where the cursor is ,if i change

Quote:

PictureBox4.SizeMode = PictureBoxSizeMode.Normal

the drawing start where it's supposed to (at the cursor position) but if i use

Quote:

PictureBoxSizeMode.Normal

and if the image i'm loading it's bigger than the picturebox4 width and heigh it looks cropped .I could use

Quote:

PictureBoxSizeMode.Autosize

but the image should not have any scrollbars visible so that why i must use zoomed because it fits the image inside the picturebox. Can someone help me figure out how to draw properly on zoomed images ? Thank you !

P.S. if there is something you did not understand please let me know .

解决方案

Don't ever draw anything on a PictureBox. This is one of the common mistakes of many beginners confused by this almost useless class, only designed to simplify the most basic graphic chores, like presenting a static image. Even though manipulating with PictureBox is possible, it will give you absolutely nothing but extra trouble, waste of development time and extra resources. In all more complex cases, you should forget it like a nightmare and render graphics directly on some custom control or even some control serving as a drawing pad, such as Panel.

I'll explain what to do. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^] (explains how it works),
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^] (this one is specifically on Zoom).

—SA


i found out how to do this right

this is the url from which i got the code
http://www.daniweb.com/software-development/vbnet/threads/151127/correctly-displaying-image-in-picturebox[^]

NOTE !! This will scale the image to "fit" the picturebox so if you decide to save the image it will save it using the scaled bitmap's resolution (if you load into your picturebox an image that has 1920x1080 you will save in a different resolution a lower one since it was scaled). To save the image with the same resolution do this :

lets say you want to load an image on form_load but before anything declare two variables like below

Quote:

Dim defaultHeight As Integer
Dim defaultWidth As Integer



Now in form_load event add this

Quote:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim PicBoxHeight As Integer
Dim PicBoxWidth As Integer
Dim ImageHeight As Integer
Dim ImageWidth As Integer
Dim TempImage As Image
Dim scale_factor As Single
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
' Get control size
PicBoxHeight = PictureBox1.Height
PicBoxWidth = PictureBox1.Width
' Load the image. Change Image.FromFile() to match your code
TempImage = Image.FromFile(str)
defaultWidth = TempImage.Width 'is the width of your selected image (unscaled)
defaultHeight = TempImage.Height 'is the height of your selected image (unscaled)
' Get image size
ImageHeight = TempImage.Height
ImageWidth = TempImage.Width
' Calculate scale_factor
scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
' 1) Height
If ImageHeight > PicBoxHeight Then
' Reduce height first
scale_factor = CSng(PicBoxHeight / ImageHeight)
End If
' 2) Width. Notice, we do know now how much we have to scale down the height
' and the scale_factor affects width too
If (ImageWidth * scale_factor) > PicBoxWidth Then
' Scaled width exceeds Box's width, recalculate scale_factor
scale_factor = CSng(PicBoxWidth / ImageWidth)
End If
' Move image to control for resizing
PictureBox1.Image = TempImage
' Get the source bitmap.
Dim bm_source As New Bitmap(PictureBox1.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, _
bm_dest.Width + 1, _
bm_dest.Height + 1)
' Display the result.
PictureBox1.Image = bm_dest
Drawbitmap = New Bitmap(PictureBox1.Image)
PictureBox1.Height = Drawbitmap.Height
Drawgraphics = Graphics.FromImage(Drawbitmap)
End Sub



Picturebox1 is just an example replace it with your picturebox
Now when you click the save button in my case it's another picturebox (you could use a button) add this to the click event

Quote:

Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
Dim bmp As New Bitmap(PictureBox1.Image, defaultWidth, defaultHeight) ' this will take the bitmap from your Picturebox1.image and scale it to it's original width and hight
bmp.Save("C:\image.jpg")
bmp.Dispose()
MessageBox.Show("Saved !")
End Sub



I hope there will be no misunderstandings when reading this . Enjoy and thank you for your replies once again.


这篇关于vb.net在缩放图像上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆