حفظ الصورة من picture box وحفظها بصيغة pdf - جيولوجي مبتدئ - 08-06-24
السلام عليكم ورحمة الله وبركاته
اريد أن اقوم بحفظ صورة من حفظ الصورة من picture box وحفظها بصيغة pdf داخل مجلد موجود على c مع العلم اني استخدم مكتبة pdf.sharp
لكم جزيل الشكر
دمتم بود
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - العتيق - 08-06-24
[attachment=29603]
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - جيولوجي مبتدئ - 08-06-24
الشكر الجزيل لك أخي العتيق ولكن عندي مشكلة في iTextSharp فعند تحميلها تظهر مشاكل كثيرة لذلك استخدم pdfSharp بدلاً منها
لك جزيل الشكر
دمت بود
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - Kamil - 08-06-24
و عليكم السلام و رحمة الله و بركاته
تفضل هذا التعديل
PHP كود :
using System; using System.Drawing; using System.Windows.Forms; using PdfSharp.Pdf; using PdfSharp.Drawing;
namespace SaveImageToPDF { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnSaveToPDF_Click(object sender, EventArgs e) { // Check if there is an image in the PictureBox if (pictureBox1.Image == null) { MessageBox.Show("There is no image in the PictureBox to save.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
// Define the path where the PDF will be saved string folderPath = @"C:\MyPDFs"; string filePath = System.IO.Path.Combine(folderPath, "Image.pdf");
// Create the folder if it does not exist if (!System.IO.Directory.Exists(folderPath)) { System.IO.Directory.CreateDirectory(folderPath); }
try { // Create a new document PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page);
// Convert the image from PictureBox to a PdfSharp image using (MemoryStream memoryStream = new MemoryStream()) { pictureBox1.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); XImage pdfImage = XImage.FromStream(memoryStream);
// Set image position and size XRect rect = new XRect(0, 0, page.Width, page.Height); gfx.DrawImage(pdfImage, rect); }
// Save the document document.Save(filePath);
MessageBox.Show("Image saved to PDF successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - جيولوجي مبتدئ - 08-06-24
الشكر الجزيل لكما اخواني على سرعة تفاعلكم وبارك الله جهودكم
لكم جزيل الشكر
دمتم بود
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - Kamil - 08-06-24
الكود السابق #C
اما كود 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
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - جيولوجي مبتدئ - 08-06-24
(08-06-24, 07:39 PM)Kamil كتب : الكود السابق #C
اما كود 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
عاجز عن الشكر استاذ شاكر وبارك الله وفيك ورزقك من حيث تحتسب وحيث لاتحتسب
عفواً أكثرت بالأسئلة بس أتعبني هذا الموضوع كثيراً وأوعدكم هذا آخر سؤال لهذا الموضوع
في السطر التالي يظهر الخطأ Combine is not member of string
Dim filePath As String = Path.Combine(folderPath, "Image.pdf")
لكم جزيل الشكر
دمتم بود
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - Taha Okla - 08-06-24
(08-06-24, 08:49 PM)جيولوجي مبتدئ كتب : عفواً أكثرت بالأسئلة بس أتعبني هذا الموضوع كثيراً وأوعدكم هذا آخر سؤال لهذا الموضوع
في السطر التالي يظهر الخطأ Combine is not member of string
Dim filePath As String = Path.Combine(folderPath, "Image.pdf")
لكم جزيل الشكر
دمتم بود
فيك تستبدله بالسطر التالي :
كود :
Dim filePath As String =folderPath & "\Image.pdf"
RE: حفظ الصورة من picture box وحفظها بصيغة pdf - جيولوجي مبتدئ - 09-06-24
السلام عليكم ورحمة وبركاته
من لايشكر الناس لايشكر الله حفظك الله استاذ طه كما أنني لا أنسى شكر الأستاذ العتيق والأستاذ كامل لقد أتعبتكم معي كثيراً
لكم جزيل الشكر
دمتم بود
|