简体   繁体   English

如何使用 vb.net 和 SQL Server 裁剪和调整图像大小

[英]How to crop and resize image using vb.net and SQL Server

I'm using vb.net and sql server 2008 and a picturebox for uploading photos.我正在使用 vb.net 和 sql server 2008 以及用于上传照片的图片框。 This is what I want to do.这就是我想要做的。

1.The image can be anything. 1.图像可以是任何东西。 It can be bitmap,jpg,png, anything 2. upload image from my computer 3. Crop the image and resize it to fit in my picture box.它可以是位图,jpg,png,任何东西 2. 从我的电脑上传图像 3. 裁剪图像并调整其大小以适合我的图片框。 4. Save it to the SQL Server database 4.保存到SQL Server数据库

This is my working code for uploading and saving images in the database.这是我在数据库中上传和保存图像的工作代码。

Public Class Form1
  Dim a As New OpenFileDialog

 Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
        Dim picl as string
        a.filter = nothing
        picl = a.filename
        a.showdialog()
        picturebox1.image = image.fromfile(a.filename)
 End Sub

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
 cn.Open()
    Using cmd As New SqlClient.SqlCommand("dbo.uspAdd", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add(New SqlClient.SqlParameter("@customerPic", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
                cmd.ExecuteNonQuery()
                MsgBox("Save Record New record Successfully")
            End Using
cn.close
End Sub

I have read some reference here but it does not work for me and now I'm stuck for almost an hour.我在这里阅读了一些参考资料,但它对我不起作用,现在我被困了将近一个小时。 This is what i have tried.这是我尝试过的。

Imports System.Drawing.Drawing2D
    Public Class Form1
    Dim a As New OpenFileDialog
    Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
        Dim picl As String
        a.Filter = Nothing
        picl = a.FileName
        a.ShowDialog()
        PictureBox1.Image = Image.FromFile(a.FileName)
        Dim OriginalImage = Image.FromFile(a.FileName)
        Dim CropRect As New Rectangle(100, 0, 100, 100)
        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()
        End Using
    End Sub

Can someone please help me to fix my code.有人可以帮我修复我的代码。 Any help would be very much appreciated.任何帮助将不胜感激。 Thanks谢谢

you can crop image and designate the path and the name of cropped image with the following code sample , anyway ur sql fucntion needs image file path您可以使用以下代码示例裁剪图像并指定裁剪图像的路径和名称,无论如何您的sql功能需要图像文件路径

  Dim LocX = 100 'x cord. of where crop starts
    Dim LocY = 0 'y  cord. of where crop starts
    Dim CropW = 100 'Crop width
    Dim CropH = 100 'Crop height
    Dim CropRect As New Rectangle(LocX, LocY, CropW, CropH)
    Dim OriginalImage = PictureBox1.Image
    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)
        CropImage.Save("cropped file path and name here")
    End Using

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

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