如何在 vb.net 中裁剪图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8725489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:50:02  来源:igfitidea点击:

How to crop an image in vb.net?

vb.netimagecrop

提问by user4951

The image can be anything. It can be jpg, png, anything.

图像可以是任何东西。它可以是 jpg、png 或任何格式。

Load it.

加载它。

Crop it. Say removing first 100 pixels from the left.

裁剪它。假设从左侧删除前 100 个像素。

Save to the same file

保存到同一个文件

回答by adatapost

Use Graphics.DrawImage Method (Image, RectangleF, RectangleF, GraphicsUnit)method.

使用Graphics.DrawImage Method(Image, RectangleF, RectangleF, GraphicsUnit)方法。

    Dim fileName = "C:\file.jpg"
    Dim CropRect As New Rectangle(100, 0, 100, 100)
    Dim OriginalImage = Image.FromFile(fileName)
    Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
    Using grp = Graphics.FromImage(CropImage)
        grp.DrawImage(OriginalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
        OriginalImage.Dispose()
        CropImage.Save(fileName)
    End Using

回答by Niranjan Singh

Ref: Graphics.DrawImageand Image Cropping with Image Resizing Using VB.NET

参考:Graphics.DrawImage使用 VB.NET 调整图像大小的图像裁剪

private void btnCropImage_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.ShowDialog();
        //check your filename or set constraint on fileopen dialog
        //to open image files
        string str = dlg.FileName;

        //Load Image File to Image Class Object to make crop operation
        Image img = System.Drawing.Bitmap.FromFile(str);

        // Create rectangle for source image, what ever it's size. 
        GraphicsUnit units = GraphicsUnit.Pixel;
        RectangleF srcRect = img.GetBounds(ref units);

        // Create rectangle for displaying image - leaving 100 pixels from left saving image size.
        RectangleF destRect = new RectangleF(100.0F, 0.0F, srcRect.Width - 100, srcRect.Height);

        // Bitmap class object to which saves croped image
        Bitmap bmp = new Bitmap((int)srcRect.Width - 100, (int)srcRect.Height);

        // Draw image to screen.
        Graphics grp = Graphics.FromImage(bmp);
        grp.DrawImage(img, destRect, srcRect, units);

        //save image to disk
        bmp.Save("e:\img.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

        //Clear memory for Unused Source Image
        img.Dispose();
    }

Hope this help you..

希望这对你有帮助..

回答by sudhakar

Region "Image Cropping"

区域“图像裁剪”

Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer

Dim oCropX As Integer
Dim oCropY As Integer
Dim cropBitmap As Bitmap

Public cropPen As Pen
Public cropPenSize As Integer = 1 '2
Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
Public cropPenColor As Color = Color.Yellow
Private Sub RotateBtn_Click(sender As System.Object, e As EventArgs) Handles RotateBtn.Click

    ' RotateImage(PreviewPictureBox.Image, offset:=, angle:=90)
    crobPictureBox.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    'PreviewPictureBox.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    '(45, PreviewPictureBox.Image)
End Sub
Private Sub crobPictureBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseDown
    Try

        If e.Button = Windows.Forms.MouseButtons.Left Then

            cropX = e.X
            cropY = e.Y

            cropPen = New Pen(cropPenColor, cropPenSize)
            cropPen.DashStyle = DashStyle.DashDotDot
            Cursor = Cursors.Cross

        End If
        crobPictureBox.Refresh()
    Catch exc As Exception
    End Try
End Sub
Dim tmppoint As Point
Private Sub crobPictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseMove
    Try

        If crobPictureBox.Image Is Nothing Then Exit Sub

        If e.Button = Windows.Forms.MouseButtons.Left Then

            crobPictureBox.Refresh()
            cropWidth = e.X - cropX
            cropHeight = e.Y - cropY
            crobPictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)
        End If
        ' GC.Collect()

    Catch exc As Exception

        If Err.Number = 5 Then Exit Sub
    End Try

End Sub

Private Sub crobPictureBox_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseUp
    Try
        Cursor = Cursors.Default
        Try

            If cropWidth < 1 Then
                Exit Sub
            End If

            Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth, cropHeight)
            Dim bit As Bitmap = New Bitmap(crobPictureBox.Image, crobPictureBox.Width, crobPictureBox.Height)

            cropBitmap = New Bitmap(cropWidth, cropHeight)
            Dim g As Graphics = Graphics.FromImage(cropBitmap)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
            ' g.DrawImage(bit, 0, 0, rect,GraphicsUnit.Pixel)
            PreviewPictureBox.Image = cropBitmap

        Catch exc As Exception
        End Try
    Catch exc As Exception
    End Try
End Sub