如何在 DataGridView 中显示圆形图像?

时间:2021-04-01 06:25:36

标签: vb.net winforms datagridview graphics

我正在尝试在 DataGridView 中显示圆形图像。
这是我正在使用的代码:

Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(*.jpg;*.'png;*.gif)|*.jpg;*.png;*.gif"

If opf.ShowDialog = DialogResult.OK Then
    If Not Directory.Exists(AvatarsDirectory) Then
        Directory.CreateDirectory(AvatarsDirectory)
    End If

    fileName = Path.GetFileName(opf.FileName)
    Dim fileSavePath As String = Path.Combine(AvatarsDirectory, fileName)
    File.Copy(opf.FileName, fileSavePath, True)
    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

所以这段代码将图像保存到“Avatar”文件夹中,该图像看起来完全像这样:

enter image description here

所以它已经另存为 PNG,您认为我的代码中缺少什么?

这是我的“插入用户信息”代码,我将其留在这里,希望对您有所帮助:

Try
    Dim mstream As New System.IO.MemoryStream()
    Pic1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Png)
    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", fileName)

    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

这是我得到的结果:

enter image description here

0 个答案:

没有答案