简体   繁体   English

更大的图像以适合图片框

[英]Larger Image to fit in picturebox

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.这就是我得到的。

在此处输入图像描述

在此处输入图像描述

If it's a winforms app, you can set the SizeMode property of the PictureBox to PictureBoxSizeMode.Zoom .如果是 winforms 应用,可以将PictureBoxSizeMode.ZoomSizeMode属性设置为PictureBox 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 .请注意,这将增加较小图像的大小以填充框架,因此您可能需要先测量图像,以检查任一边缘是否太长,然后将SizeMode设置为PictureBoxSizeMode.ZoomPictureBoxSizeMode.Normal

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结束子

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 BackgroundImage property of the PictureBox - 使用 PictureBox 的BackgroundImage属性

        picturebox.BackgroundImage = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))

-Then Set its BackgroundImageLayout to stretch Like This: - 然后将其 BackgroundImageLayout 设置为像这样拉伸

        picturebox.BackgroundImageLayout = ImageLayout.Stretch
  1. Image -use the Image property of the PictureBox Image - 使用 PictureBox 的Image属性

    picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))

-Then Set its' sizeMode to StretchImage Like 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这将使任何图片/图像/Canvas Stroke(转换为字节数组)适合图片框的高度和宽度

Hope This Helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM