13-12-17, 10:44 PM
لا يمكن حذف صورة من أداة PictureBox1 تم عرضها بواسطة الخاصية Image أو Load حتى لو أفرغت الأداة من الصورة
لهذا الأفضل استخدام الخاصية ImageLocation للأداة PictureBox1
هذا تعديل للكود
تبقى نقطة وهي التأكد من عدم حذف الصورة التي تم اختيارها كخلفية لسطح المكتب
لهذا الأفضل استخدام الخاصية ImageLocation للأداة PictureBox1
هذا تعديل للكود
PHP كود :
Imports System.Data.OleDb
Imports System.IO
'Add Background
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class Form1
''' <summary>
''' ''''''''''''''''
''' </summary>
''' <param name="uAction"></param>
''' <param name="uParam"></param>
''' <param name="lpvParam"></param>
''' <param name="fuWinIni"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
Private Const SPI_SETDESKWALLPAPER = 20
Private Const SPIF_UPDATEINIFILE = &H1
''' <summary>
''' ''''''''''''''''''''''''
''' </summary>
''' <remarks></remarks>
Dim myImageFolder As String = Application.StartupPath & "\Pic\"
Public cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & " Data Source = " & Application.StartupPath & "\Database1.mdb")
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.ReadOnly = True
Me.DataGridView1.MultiSelect = False
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
Loaddt()
End Sub
Private Sub Loaddt()
dt.Clear()
Dim da As New OleDbDataAdapter("SELECT * FROM [Table1]", cn)
da.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "imge|*.img;*.bmp;*.png;*.jpg"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
''''''''''''''''
TextBox1.Text = OpenFileDialog1.FileName
Dim PicName As String = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
Dim Picimge As String = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
If IO.File.Exists(myImageFolder & Picimge) Then
MsgBox("الصورة موجود سابقاً")
Else
My.Computer.FileSystem.CopyFile(OpenFileDialog1.FileName, myImageFolder & Picimge)
Savedt(PicName, Picimge)
End If
End If
End Sub
Private Sub Savedt(ByVal namefirst As String, ByVal imagelast As String)
Dim cm As New OleDbCommand("INSERT INTO [Table1] ([Nameimage], [imagepath]) VALUES (@Nameimage, @imagepath)", cn)
cm.Parameters.AddWithValue("@Nameimage", namefirst)
cm.Parameters.AddWithValue("@imagepath", imagelast)
If cn.State <> ConnectionState.Open Then cn.Open()
cm.ExecuteNonQuery()
cn.Close()
Loaddt()
MsgBox("تم حفظ الصورة")
End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
If BindingContext(dt).Position <> -1 Then
Dim Position As Integer = Me.BindingContext(dt).Position
PictureBox1.ImageLocation = (myImageFolder & dt.Rows(Position).Item("imagepath"))
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ''''''''''''
Dim imagePath As String = Application.StartupPath & "\myNewWallpaper.bmp"
PictureBox1.Image.Save(imagePath, ImageFormat.Bmp)
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imagePath, SPIF_UPDATEINIFILE)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dlg As OpenFileDialog = New OpenFileDialog
dlg.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"
dlg.Title = "Select the image to load."
dlg.ShowDialog()
PictureBox1.ImageLocation = (dlg.FileName)
dlg.Dispose()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
Dim imagePath As String = myImageFolder & Me.DataGridView1.CurrentRow.Cells(2).Value
My.Computer.FileSystem.DeleteFile(imagePath)
Dim cm As New OleDbCommand("DELETE FROM [Table1] WHERE ([ID]=@id)", cn)
cm.Parameters.AddWithValue("@id", Val(Me.DataGridView1.CurrentRow.Cells(0).Value))
If cn.State <> ConnectionState.Open Then cn.Open()
cm.ExecuteNonQuery()
Loaddt()
MsgBox("تم حذف الصورة")
Catch ex As Exception
MsgBox(ex.Message)
Finally
cn.Close()
End Try
End Sub
End Class

