تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
حفظ الصورة من picture box وحفظها بصيغة pdf
#6
الكود السابق #C

اما كود vb.net فهو التالي

PHP كود :
Imports System.IO
Imports PdfSharp
.Pdf
Imports PdfSharp
.Drawing

Public Class Form1
    Private Sub btnSaveToPDF_Click
(sender As ObjectAs EventArgsHandles 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(00page.Widthpage.Height)
 
               gfx.DrawImage(pdfImagerect)
 
           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 
الرد }}}
تم الشكر بواسطة: جيولوجي مبتدئ , Taha Okla


الردود في هذا الموضوع
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - بواسطة Kamil - 08-06-24, 07:39 PM


التنقل السريع :


يقوم بقرائة الموضوع: