图像缩放与像素化

时间:2014-02-18 12:58:32

标签: vb.net winforms image

我在轨道栏上使用此代码缩放图片框中的图片。

Private Sub tbrZoomLevel_Scroll(sender As System.Object, e As System.EventArgs) Handles tbrZoomLevel.Scroll
    With pbImage
        .SuspendLayout()
        .Width = actualSize.Width * tbrZoomLevel.Value
        .Height = actualSize.Height * tbrZoomLevel.Value
        .ResumeLayout()
    End With
End Sub

pbImage是一个PictureBox控件,其sizemode为zoom。 actualSize是pbImage中图像的原始大小。

当我放大时,我得到没有像素化的图像。但我希望它能完全像素化并在缩放时显示MS Paint中显示的图像。任何帮助表示赞赏。欢迎使用VB.Net代码,C#代码或任何算法。

1 个答案:

答案 0 :(得分:3)

在你的图片框中画画:

Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

    e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    'Draw the image
End Sub

编辑:试试这个

Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim srcRect As RectangleF = New Rectangle(0, 0, PictureBox1.Width / 8, PictureBox1.Height / 8)
    Dim dstRect As RectangleF = New RectangleF(0, 0, PictureBox1.Width, PictureBox1.Height)

    e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    e.Graphics.DrawImage(pctBoxImage, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub

pctBoxImagebitmap800% zoomed

Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.ScaleTransform(8, 8)
    e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
    e.Graphics.DrawImage(pctBoxImage, 0, 0)
End Sub

0,0是位图左上角位置的坐标

瓦尔特