如何使用VB.NET和SQL服务器裁剪和调整图像大小 [英] 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数据库
这是我在数据库中上传和保存图像的工作代码。
I'm using vb.net and sql server 2008 and a picturebox for uploading photos. This is what I want to do.
1.The image can be anything. 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. 4. Save it to the SQL Server database
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
我尝试了什么:
我在这里读了一些参考资料,但它对我不起作用,现在我被困了将近一个小时。这就是我所尝试过的。
What I have tried:
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
推荐答案
意思是,它不会裁剪图像。
嗯......是的。
但是......因为你将CropImage声明为事件处理程序方法的本地,当方法退出时,裁剪的图像会被丢弃。
添加第二个图片框,并在Dispose OriginalImage后将CropImage指定给它的Image属性。你会明白我的意思......
"Meaning, it do not crop the image."
Well...yes it does.
But...because you declare CropImage as local to the event handler method, the cropped image is thrown away when the method exits.
Add a second picture box, and assign the CropImage to it's Image property after you Dispose the OriginalImage. You'll see what I mean...
这篇关于如何使用VB.NET和SQL服务器裁剪和调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!