2021年3月7日星期日

VB.NET How to remove the color Black Bakground from Image in datagridview

So what i did Here is When i select Image

Then load it to the Picturebox (Pic1)

tHEN Click Saved the Image it will be saved to database

Then it will load to datagridview

Here is my Select Image COde

 Dim opf As New OpenFileDialog          opf.Filter = "Choose Image(*.jpg;*.'png;*.gif)|*.jpg;*.png;*.gif"          If opf.ShowDialog = DialogResult.OK Then              Pic1.Image = Image.FromFile(opf.FileName)              Dim originalImage = Pic1.Image              Dim croppedImage As New Bitmap(originalImage.Width, originalImage.Height)              Using g = Graphics.FromImage(croppedImage)                  Dim path As New GraphicsPath                  path.AddEllipse(0, 0, croppedImage.Width, croppedImage.Height)                  Dim reg As New Region(path)                  g.Clip = reg                  g.DrawImage(originalImage, Point.Empty)              End Using              Pic1.Image = croppedImage          End If  

And saved it to Datagridview the Image in Datagridview Becomes Circle But the Background Became white

Here is my Savved Image to Datagridview

  Try                Dim mstream As New System.IO.MemoryStream()              Pic1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)              arrImage = mstream.GetBuffer()              Dim FileSize As UInt32              FileSize = mstream.Length                mstream.Close()              conn.ConnectionString = Myconnection              conn.Open()              sql = "INSERT INTO tblstudent(FNAME,LNAME, COURSE, YR, AGE, EMAIL,studimg) VALUES (@FNAME, @LNAME, @COURSE, @YR, @AGE, @EMAIL,@studimg)"              cmd.Connection = conn              cmd.CommandText = sql              cmd.Parameters.AddWithValue("@FNAME", TXTFIRSTNAME.Text)              cmd.Parameters.AddWithValue("@LNAME", txtlname.Text)              cmd.Parameters.AddWithValue("@COURSE", txtcourse.Text)              cmd.Parameters.AddWithValue("@YR", txtyr.Text)              cmd.Parameters.AddWithValue("@AGE", txtage.Text)              cmd.Parameters.AddWithValue("@EMAIL", txtemail.Text)              cmd.Parameters.AddWithValue("@studimg", arrImage)              Dim r As Integer              r = cmd.ExecuteNonQuery()              If r > 0 Then                  MsgBox("Student Record hass been Saved!")              Else                  MsgBox("No record has been saved!")              End If              cmd.Parameters.Clear()              conn.Close()          Catch ex As Exception              MessageBox.Show(ex.Message)          End Try  

And Here is my Load to Datagridview code

   Try              conn = New MySqlConnection              conn.ConnectionString = Myconnection              conn.Open()              sql = "SELECT * FROM tblstudent;"              cmd = New MySqlCommand(sql, conn)              da = New MySqlDataAdapter              dt = New DataTable              da.SelectCommand = cmd              da.Fill(dt)              conn.Close()              DataGridView1.DataSource = dt          Catch ex As MySqlException              MsgBox(ex.Message)          Finally              conn.Close()              da.Dispose()          End Try  

And Here is the result enter image description here

How do I make the Black Go Away? I already set the back color of Picturebox(Pic1) to white and Transparent

https://stackoverflow.com/questions/66524045/vb-net-how-to-remove-the-color-black-bakground-from-image-in-datagridview March 08, 2021 at 11:45AM

没有评论:

发表评论