为什么GDI +错误而不更新图像? [英] Why GDI+ error while not updating image ?

查看:50
本文介绍了为什么GDI +错误而不更新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我很久以前就做了一个迷你项目。首先,我要感谢所有codeproject老年人在我的项目期间帮助我并仍然提供帮助。



我正在检查我的项目但是我意识到教师资料部分有一些错误。



错误是:GDI ++中发生了一般性错误



我开始检查我的代码,但没有任何可疑之处。问题出现在

Profile_Update表格中。



如果我尝试更新名称或其他细节等详细信息,那么我只会收到这个错误。现在,如果我更新名称,其他和个人资料图片,那么我没有得到错误和数据库更新。



所以在实际情况下,个人资料所有者大多不定期更新他的图片。但我正在寻找真正的解决方法。



我的更新字符串是

Well I'm doing a mini project from long time ago. First of all I would like to thanks all codeproject seniors for helping me during my project and still helping.

I was checking my project But I realize that the faculty profile section has some errors.

The Error is: A generic error occured in GDI++

I started checking my code but there nothing suspicious. The problem arise in
Profile_Update form.

If I trying to update the with some details like name or other details, only then Im receiving this error. Now if I update name, other and profile picture then Im not getting error and Database updating.

So In real case Profile owner mostly doesnt update his picture periodically. But I m looking for real fix about this.

My Update String is

="SELECT * from [USERDATA] name=@name,dob=@dob,sex=@sex,addr=@addr,phone=@phone,avatar=@avatar WHERE [ID]='" & 
 IDBOX.Text & "'"
update.Parameters.AddWithValues("@name", SqlDbType.VarChar).Value = NameHere.Text
update.Parameters.AddWithValues("@dob", SqlDbType.VarChar).Value = DobSelect.Value.Date
update.Parameters.AddWithValues("@sex", SqlDbType.Int).Value = GenBox.Text
update.Parameters.AddWithValues("@addr", SqlDbType.VarChar).Value = AddrBox.Text
update.Parameters.AddWithValues("@phone", SqlDbType.VarChar).Value = Mobile.Text
update.Parameters.AddWithValues("@name", SqlDbType.VarChar).Value = NameHere.Text
Dim ms As New System.IO.MemoryStream
AvatarBox.Image.Save(ms,AvatarBox.Image.RawFormat)
update.Parameters.AddWithValues("@avatar", SqlDbType.Image).Value = ms.ToArrey
If update.ExecuteNonQuery() =1 Then
MsgBox("Faculty Update")



工作如果我在AvatarBox中选择新图片,则抛出错误GDI ++。

如果我更改了某些字段,如名称,dob等等,则会抛出错误。

如果我更新所有内容,它的工作没有错误。



任何修复这个。希望我能让你明白。



我的教师更新表格还有一个名为FacultyDGV CellClick事件的DataGridView填充。

如果我单击FacultyDGV中的某些字段,然后form2将自动打开并填充数据。



我可以添加新图像而不会出现任何错误但更新无效。



谢谢代码项目..



我尝试了什么:




Its working If I select new picture in AvatarBox else its throwing error GDI++.
Its throwing error if I change some fields like name, dob etc etc.
Its worrking without error if I update everything.

Any fix for this. Hopefully I make you understand.

One more thing is My Faculty Update Form is being populated by a DataGridView named FacultyDGV CellClick event.
If I click some fields in FacultyDGV then the form2 will opens automatically with populated data.

I can add new Image without any single error but Updating is not working.

Thankyou code project..

What I have tried:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If TextBox2.Text = "" Then
                Label25.Text = "* Faculty Name cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf ComboBox1.Text = "< Select >" Then
                Label25.Text = "* Gender must be selected."
                Label25.ForeColor = Color.Red
            ElseIf TextBox3.Text = "" Then
                Label25.Text = "* Address cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox4.Text = "" Then
                Label25.Text = "* District cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox5.Text = "" Then
                Label25.Text = "* State cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox14.Text = "" Then
                Label25.Text = "* Pin cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox6.Text = "" Then
                Label25.Text = "* Country cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox8.Text = "" Then
                Label25.Text = "* Contact No cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox10.Text = "" Then
                Label25.Text = "* Username cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox11.Text = "" Then
                Label25.Text = "* Password cannot be left blank."
                Label25.ForeColor = Color.Red
            ElseIf TextBox11.Text <> TextBox12.Text Then
                Label25.Text = "* Please, Check the passwords you've entered."
                Label25.ForeColor = Color.Red
            ElseIf ComboBox2.Text = "< Select Question >" Then
                Label25.Text = "* Security Question must be selected."
                Label25.ForeColor = Color.Red
            Else
                OpenConnection()
                Label25.Text = "* Updating your profile..."
                Label25.ForeColor = Color.Black
                Dim cmd2 As New SqlCommand("UPDATE [userdata] SET name=@name,dob=@dob,sex=@sex,addr=@addr,city=@city,state=@state,pin=@pin,country=@country,nat=@nat,mobile=@mobile,email=@email,dept=@dept,sub=@sub,username=@username,password=@password,security=@security,answer=@answer,avatar=@avatar,modified=@modified WHERE id='" & TextBox1.Text & "'", connection)
                cmd2.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = TextBox2.Text
                cmd2.Parameters.AddWithValue("@dob", SqlDbType.VarChar).Value = DateTimePicker1.Value.Date
                Dim gender As String
                Dim value As Integer
                gender = ComboBox1.Text
                If gender = "Male" Then
                    value = 1
                ElseIf gender = "Female" Then
                    value = 2
                ElseIf gender = "Others" Then
                    value = 3
                End If
                cmd2.Parameters.AddWithValue("@sex", SqlDbType.Int).Value = value
                cmd2.Parameters.AddWithValue("@addr", SqlDbType.VarChar).Value = TextBox3.Text
                cmd2.Parameters.AddWithValue("@city", SqlDbType.VarChar).Value = TextBox4.Text
                cmd2.Parameters.AddWithValue("@state", SqlDbType.VarChar).Value = TextBox5.Text
                cmd2.Parameters.AddWithValue("@pin", SqlDbType.VarChar).Value = TextBox14.Text
                cmd2.Parameters.AddWithValue("@country", SqlDbType.VarChar).Value = TextBox6.Text
                cmd2.Parameters.AddWithValue("@nat", SqlDbType.VarChar).Value = TextBox7.Text
                cmd2.Parameters.AddWithValue("@mobile", SqlDbType.VarChar).Value = TextBox8.Text
                cmd2.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = TextBox9.Text
                cmd2.Parameters.AddWithValue("@dept", SqlDbType.VarChar).Value = ComboBox3.Text
                cmd2.Parameters.AddWithValue("@sub", SqlDbType.VarChar).Value = ComboBox4.Text
                cmd2.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = TextBox10.Text
                cmd2.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = TextBox11.Text
                Dim question As String
                Dim data As Integer
                question = ComboBox2.Text
                If question = "Whats your nickname ?" Then
                    data = 5
                ElseIf question = "Whats your pet name ?" Then
                    data = 4
                ElseIf question = "Whats your birth place ?" Then
                    data = 3
                ElseIf question = "Whats your middle name ?" Then
                    data = 2
                ElseIf question = "Whos your childhood hero ?" Then
                    data = 1
                End If
                cmd2.Parameters.AddWithValue("@security", SqlDbType.Int).Value = data
                cmd2.Parameters.AddWithValue("@answer", SqlDbType.VarChar).Value = TextBox13.Text
                cmd2.Parameters.AddWithValue("@modified", SqlDbType.VarChar).Value = Date.Today
                Dim ms As New MemoryStream
                PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
                cmd2.Parameters.AddWithValue("@avatar", SqlDbType.Image).Value = ms.ToArray
                If cmd2.ExecuteNonQuery() = 1 Then
                    MsgBox("Updated")
                End If
                connection.Close()
                faculty_dgv()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

推荐答案

试试这个:



Try this:

using (MemoryStream ms = new MemoryStream())
{
    PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat);
    byte[] arrImage = ms.GetBuffer();
    ms.Close();
    cmd2.Parameters.AddWithValue("@avatar", SqlDbType.Image).Value = arrImage;
}


这篇关于为什么GDI +错误而不更新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆