2016-04-24 74 views
-1

我正在使用vb.net和sql server 2008和一個picturebox上傳照片。 這就是我想要做的。如何使用vb.net和SQL Server來裁剪和調整圖像大小

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 

有人可以幫我解決我的代碼。任何幫助將非常感激。由於

+0

什麼是不工作...你能具體點嗎?爲什麼你有'Dim a As OpenFileDialog'兩次?如果您要裁剪圖片框中的圖片,請在將圖片添加到數據庫而不是原始圖片文件時使用該圖片。 –

+0

@chase Rocker對不起,因爲新的OpenFileDialog只聲明一次。我修正了上面的代碼。我讀過[參考](http://stackoverflow.com/questions/8725489/how-to-crop-an-image-in-vb-net),但我不能將其應用於我的代碼。你能幫我解決我的問題嗎? –

+0

您不明白特定的代碼行或者您不明白其中的任何代碼?如果你不明白這一點,你可能需要閱讀.net圖形功能......試圖解釋它在這裏是一個主題過於寬泛。 –

回答

0

可以裁剪圖像並指定路徑和裁剪後的圖像與下面的代碼示例的名字,反正烏爾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 
+0

嗨,我這樣做[代碼](http://pastebin.com/h4jYmdnA),但它會引發錯誤'在GDI +中發生的一般性錯誤''CropImage.Save(「C:\ Users \ agent_edx44 \圖片「)' –

+0

GDI +錯誤說你」你沒有驗證將這個文件保存到這個目錄「。你必須輸入文件名,比如「C:\ Users \ agent_edx44 \ Pictures \ asdf.jpg」 – snoopcommands

+0

當我輸入文件名時,錯誤仍然是一樣的。這裏是我的[截圖](http://img0.uploadhouse.com/fileuploads/22369/22369880cc6b83f87f8381f54eef8c26c6f44f63.jpg) –