私はデータベースにテーブルを持っています(SQLサーバ)イメージカラムを含んでいます。データベースに挿入する前にイメージを圧縮する方法は? 私は何が必要なのか何か解決策があれば私は喜んでいます。これは私が必要なものです: 私は賃貸店のための小さなプログラムを作っているので、ユーザーは契約書を印刷し、彼はいつでもそれを取り戻すことができるようにそれを再び保存する。私は画像の列を使用しているユーザーが画像として契約のコピーを持つことができますが、問題は、契約は2ページですので、私は2つの画像が必要とこれは多くのスペースを取る契約です。 はそれを延期する方法があります。私は、プログラムに接続されているすべてのコンピュータにサーバーを使用しています。イメージを圧縮してデータベースに挿入する前に
これは、画像を挿入するためのコードです:
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
Dim fName As String
fName = imagepath
If File.Exists(fName) Then
Dim content As Byte() = ImageToStream(fName)
'فحص الاتصال بقاعدة البيانات
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "insert into test (image) values(@image)"
cmd.Parameters.AddWithValue("@image", (content))
cmd.Connection = SQL.conn
cmd.ExecuteNonQuery()
SQL.conn.Close()
Else
MsgBox(fName & " الصورة المختارة ليست موجودة او غير صالحة ", vbCritical, "حصل خطأ")
End If
End Sub
Dim imagepath As String
Dim myStream As IO.Stream = Nothing
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim openFileDialog1 As New OpenFileDialog()
'Set the Filter.
openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
imagepath = openFileDialog1.FileName
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
Else
openFileDialog1.FileName = Nothing
Return
End If
End Sub
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return stream.ToArray()
End Function
あなたのアドバイスありがとうございます –