繁体   English   中英

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

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

我正在使用 vb.net 和 sql server 2008 以及用于上传照片的图片框。 这就是我想要做的。

1.图像可以是任何东西。 它可以是位图,jpg,png,任何东西 2. 从我的电脑上传图像 3. 裁剪图像并调整其大小以适合我的图片框。 4.保存到SQL Server数据库

这是我在数据库中上传和保存图像的工作代码。

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

我在这里阅读了一些参考资料,但它对我不起作用,现在我被困了将近一个小时。 这是我尝试过的。

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

有人可以帮我修复我的代码。 任何帮助将不胜感激。 谢谢

您可以使用以下代码示例裁剪图像并指定裁剪图像的路径和名称,无论如何您的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