08-06-24, 07:39 PM
الكود السابق #C
اما كود vb.net فهو التالي
اما كود vb.net فهو التالي
PHP كود :
Imports System.IO
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Public Class Form1
Private Sub btnSaveToPDF_Click(sender As Object, e As EventArgs) Handles btnSaveToPDF.Click
' التأكد من وجود صورة في PictureBox
If pictureBox1.Image Is Nothing Then
MessageBox.Show("لا توجد صورة في الـ PictureBox للحفظ.", "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
' تحديد مسار حفظ ملف PDF
Dim folderPath As String = "C:\MyPDFs"
Dim filePath As String = Path.Combine(folderPath, "Image.pdf")
' إنشاء المجلد إذا لم يكن موجودًا
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Try
' إنشاء مستند PDF جديد
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' تحويل الصورة من PictureBox إلى صورة PdfSharp
Using memoryStream As New MemoryStream()
pictureBox1.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png)
Dim pdfImage As XImage = XImage.FromStream(memoryStream)
' تحديد موقع وحجم الصورة
Dim rect As New XRect(0, 0, page.Width, page.Height)
gfx.DrawImage(pdfImage, rect)
End Using
' حفظ المستند
document.Save(filePath)
MessageBox.Show("تم حفظ الصورة في ملف PDF بنجاح!", "نجاح", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("حدث خطأ: " & ex.Message, "خطأ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
