vb.net 更大的图像以适合图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7030691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Larger Image to fit in picturebox
提问by Fredrik M?rk
As you'll can see the first image is the size of (1024*768) and it is correctly displayed in the picturebox and in the second case the image size is (1600*900) and it is displayed to half of the picturebox and the remaining is missing .So No I would like to fir that image in the picturebox no matter what the size is and even though it is greater than the size of the picturebox.I need to scale that Image.So how do I do that?And one more thing is that I need to resize the picturebox automatically when the image loads to it just as we see in the lightbox effect.. http://www.lokeshdhakar.com/projects/lightbox2/-------->example.
如您所见,第一张图片的大小为 (1024*768) 并且正确显示在图片框中,在第二种情况下,图片大小为 (1600*900) 并显示到图片框的一半和剩下的丢失了。所以不,无论大小是多少,即使它大于图片框的大小,我都想在图片框中激活该图像。我需要缩放该图像。那么我该怎么做?还有一件事是我需要在图像加载到它时自动调整图片框的大小,就像我们在灯箱效果中看到的一样.. http://www.lokeshdhakar.com/projects/lightbox2/------- -> 例子。
Any help will be appreciated!
任何帮助将不胜感激!
Here is what I am getting.
这是我得到的。
回答by Fredrik M?rk
If it's a winforms app, you can set the SizeMode
property of the PictureBox
to PictureBoxSizeMode.Zoom
. Note that this will increase the size of smaller images to fill the frame, so you might want to measure the image first, in order to check if either edge is too long, and then setting SizeMode
to either PictureBoxSizeMode.Zoom
or PictureBoxSizeMode.Normal
.
如果它是一个 winforms 应用程序,您可以将 的SizeMode
属性设置PictureBox
为PictureBoxSizeMode.Zoom
。请注意,这将增加较小图像的大小以填充框架,因此您可能需要先测量图像,以检查任一边缘是否太长,然后设置SizeMode
为PictureBoxSizeMode.Zoom
或PictureBoxSizeMode.Normal
。
回答by Fredrik M?rk
I know this is marked answered, but I wrote this for one of my own apps. Hope it helps somebody..
我知道这被标记为已回答,但我是为我自己的一个应用程序编写的。希望它可以帮助某人..
Private Sub ScaleImage(ByVal p As PictureBox, ByRef i As Bitmap)
If i.Height > p.Height Then
Dim diff As Integer = i.Height - p.Height
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
If i.Width > p.Width Then
Dim diff As Integer = i.Width - p.Width
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
End Sub
结束子
回答by Myk Agustin
The two Easiest Ways to Fit an Image to Any Size of PictureBox is:
使图像适合任何尺寸的 PictureBox 的两种最简单方法是:
-to set the Image as Background Image OR -to set it as picturebox image then set sizemode to StretchImage
- 将图像设置为背景图像或 - 将其设置为图片框图像,然后将 sizemode 设置为 StretchImage
1.Background Image
1.背景图片
-use the BackgroundImageproperty of the PictureBox
- 使用PictureBox的BackgroundImage属性
picturebox.BackgroundImage = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its BackgroundImageLayout to stretchLike This:
- 然后将其 BackgroundImageLayout 设置为这样拉伸:
picturebox.BackgroundImageLayout = ImageLayout.Stretch
Image -use the Imageproperty of the PictureBox
picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
Image - 使用PictureBox的Image属性
picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its' sizeMode to StretchImageLike This:
- 然后将其'sizeMode 设置为 StretchImage,如下所示:
picturebox.SizeMode = PictureBoxSizeMode.StretchImage
This will make any Picture / Image / Canvas Stroke (converted to Byte Array) fit according to the height and width of the picturebox
这将使任何图片/图像/画布描边(转换为字节数组)根据图片框的高度和宽度适合
Hope This Helps :)
希望这可以帮助 :)