图像不适合picturebox

时间:2017-07-13 15:25:46

标签: vb.net image-processing

我有一个winforms程序扫描文档并将其保存到文件中,然后打开另一个表单并将图像的裁剪部分加载到图片框中,但图像不会填满图片框。

执行此操作的代码如下:

Public Function Crop() As Image

    ' Function to rotate and crop the scanned image to speed up barcode reading

    Dim Mystream As New FileStream(TempRoot & StrFileName & Exten, FileMode.Open)
    Dim bitmap1 As New Bitmap(Mystream)
    imageAttr1.SetGamma(2.2F)

    ' Rotates and crops the scanned document

    Try

        If bitmap1 IsNot Nothing Then
            bitmap1.RotateFlip(RotateFlipType.Rotate270FlipNone)
        End If

        cropX = 1500
        cropY = 200
        cropWidth = 1100
        cropHeight = 550

        ' Sets a rectangle to display the area of the source image

        rect = New Rectangle(cropX, cropY, cropWidth, cropHeight)

        ' Create a new bitmap with the width and height values specified by cropWidth and cropHeight.

        cropBitmap = New Bitmap(cropWidth, cropHeight)

        ' Creates a new Graphics object that will draw on the cropBitmap

        g = Graphics.FromImage(cropBitmap)

        ' Draws the portion of the image that you supplied cropping values for.

        g.DrawImage(bitmap1, 0, 0, rect, GraphicsUnit.Pixel)
        g.DrawImage(cropBitmap, rect, 0, 0, OKTickets.ImgTicket.Width, OKTickets.ImgTicket.Height, GraphicsUnit.Pixel, imageAttr1)

    Catch ex As System.IO.FileNotFoundException

        MessageBox.Show("There was an error. Check the path to the bitmap.")

    End Try

    Mystream.Close()

    Return cropBitmap

End Function

我在表单上有一个标签,显示宽度和宽度。图像的高度以及图片框的宽度和高度。

图片框的大小是宽度= 1100&身高= 550。

图像显示的尺寸相同,但只填充图片框的左上角。

我尝试将图片框大小模式设置为所有设置,但它对图像没有任何影响。

有人能看出为什么它没有填充图片框吗?

1 个答案:

答案 0 :(得分:1)

我相信您遇到了扩展问题 您指示扫描源图像。很可能是以高分辨率扫描图像。创建新Bitmap时,其默认分辨率为96 x 96。

来自您正在使用的DrawImage方法的Remarks部分。

  

图像存储像素宽度的值和水平分辨率的值(每英寸点数)。图像的物理宽度(以英寸为单位)是像素宽度除以水平分辨率。例如,像素宽度为360且水平分辨率为每英寸72点的图像具有5英寸的物理宽度。类似的评论适用于像素高度和物理高度。

     

此方法使用其物理大小绘制图像的一部分,因此   无论如何,图像部分都将以英寸为单位   显示设备的分辨率(每英寸点数)。例如,   假设图像部分具有216的像素宽度和水平   分辨率为每英寸72点。如果你调用这个方法来绘制它   设备上的图像部分,每英寸分辨率为96点,   渲染图像部分的像素宽度为(216/72)* 96 =   288。

您有两个选项可以解决此问题。

  1. 您可以将cropBitmap的分辨率设置为与源图像匹配。

    cropBitmap.SetResolution(bitmap1.Horizo​​ntalResolution,bitmap1.VerticalResolution)

  2. 使用DrawImageUnscaled方法。

    g.DrawImageUnscaled(bitmap1,-rect.Left,-rect.Top,0,0)

相关问题