تقييم الموضوع :
  • 1 أصوات - بمعدل 5
  • 1
  • 2
  • 3
  • 4
  • 5
كلاس بسيط لطباعة صورة الفورم
#1
الكود التالي يوضح شكل كلاس بسيط يمكن استخدامه لطباعة صورة الفورم و ذلك بإستخدام مقياس رسم يتناسب مع حجم ورقة الطباعة

PHP كود :
Imports System.ComponentModel
Imports System
.Drawing.Printing

Public Class PrintForm
    Implements IDisposable

    Private formtoPrint 
As Form
    Private formBackcolor 
As Color
    Private formImage 
As Bitmap
    Private _printDocument 
As PrintDocument
    Private _disposed 
As Boolean

    Public Sub 
New(As Form)
 
       Me.New(fColor.White)
 
   End Sub

    Private Sub 
New(As FormbackColor As Color)
 
       formtoPrint f
        formBackcolor 
backColor
    End Sub

    
<Description("Print Document"), Bindable(False), Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
 
   Public Property PrintDocument() As PrintDocument
        Get
            If Me
._printDocument Is Nothing Then
                Me
._printDocument = New PrintDocument()
 
               AddHandler Me._printDocument.PrintPage, New PrintPageEventHandler(AddressOf Me.pd_PrintPage)
 
           End If
 
           Return Me._printDocument
        End Get
        Set
(value As PrintDocument)
 
           Me._printDocument value
        End Set
    End Property

    Public Property BackColor 
As Color
        Get
            Return Me
.formBackcolor
        End Get
        Set
(value As Color)
 
           Me.formBackcolor value
        End Set
    End Property

    Public ReadOnly Property Form 
As Form
        Get
            Return formtoPrint
        End Get
    End Property

    Public ReadOnly Property Image 
As Bitmap
        Get
            If Me
.formImage Is Nothing Then
                Me
.formImage = New Bitmap(Me.GetFormImage())
 
           End If
 
           Return Me.formImage
        End Get
    End Property

    Friend 
Function CanInvokeForm(As Form) As Boolean
        Return f IsNot Nothing AndAlso Not f
.IsDisposed AndAlso f.IsHandleCreated
    End 
Function

 
   Private Function GetFormImage() As Bitmap
        Dim bmp 
As Bitmap Nothing
        If CanInvokeForm
(Me.FormThen
            bmp 
= New Bitmap(Me.Form.WidthMe.Form.Height)
 
           Dim bounds As Rectangle Me.Form.Bounds
            bounds
.Offset(-bounds.X, -bounds.Y)
 
           Me.Form.DrawToBitmap(bmpbounds)
 
       End If
 
       Return bmp
    End 
Function

 
   Private Sub pd_PrintPage(sender As ObjectAs PrintPageEventArgs)
 
       Paint(e.Graphicse.MarginBounds500)
 
   End Sub

    Private Sub Paint
(As Graphicsbounds As RectanglemarginX As IntegermarginY As Integer)
 
       If Me.Image Is Nothing Then
            Return
        End 
If

 
       Dim rect As Rectangle = New Rectangle(bounds.Xbounds.Ybounds.Widthbounds.Height)
 
       Dim ratio As Double Math.Max(CDbl(Me.Image.Width) / rect.WidthCDbl(Me.Image.Height) / rect.Height)
 
       Dim x As Single CSng(rect.Width ratio Me.Image.Width 2)
 
       Dim y As Single CSng(rect.Height ratio Me.Image.Height 2)
 
       Dim mx As New Drawing2D.Matrix(1.0F CSng(ratio), 001.0F CSng(ratio), 00)
 
       mx.Translate(xy)
 
       g.Transform mx
        g
.DrawImageUnscaled(Me.ImagemarginXmarginY)

 
   End Sub

    Public Sub 
Print(show As Boolean)
 
       If show Then
            Dim result 
As DialogResult = New PrintDialog() With {.Document Me.PrintDocument}.ShowDialog()
 
           If result <> DialogResult.OK AndAlso result <> DialogResult.Yes Then
                Return
            End 
If
 
       End If
 
       Me.PrintDocument.Print()
 
   End Sub

    Public Sub ShowPrintPreview
()
 
       Dim previewDialog As PrintPreviewDialog = New PrintPreviewDialog() With {.Document Me.PrintDocument}
 
       previewDialog.ShowDialog()
 
   End Sub

    Public Sub ShowPageSetup
()
 
       Dim setupDialog As PageSetupDialog = New PageSetupDialog() With {.Document Me.PrintDocument}
 
       setupDialog.ShowDialog()
 
   End Sub

    Protected Overridable Sub Dispose
(disposing As Boolean)
 
       If _disposed Then
            Return
        End 
If
 
       If disposing Then
            If Me
.formImage IsNot Nothing Then
                Me
.formImage.Dispose()
 
               Me.formImage Nothing
            End 
If
 
       End If
 
       _disposed True
    End Sub

    
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
    '
Protected Overrides Sub Finalize()
 
   '    ' Do not change this code Put cleanup code in Dispose(disposing As Booleanabove.
 
   '    Dispose(False)
    ' 
   MyBase.Finalize()
 
   'End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        ' 
TODOuncomment the following line if Finalize() is overridden above.
 
       ' GC.SuppressFinalize(Me)
    End Sub

End Class 

الإستخدام
أضف الكلاس لمشروعك ثم استخدم الكود بالشكل التالي


PHP كود :
       Using pf As New PrintForm(Me)
 
           pf.ShowPrintPreview()
 
           pf.Print(False)
 
       End Using 


أتمني أن يكون الكلاس مفيدا للبعض منكم
و يمكنكم أن تضعوا اي استفسار في حالة إن لم يكن الكود غير واضحا للبعض منكم
الرد }}}
تم الشكر بواسطة: abdualla , abdualla , samira20
#2
تسلم يدك

الله يجزيك الف خير
الرد }}}
تم الشكر بواسطة:
#3
فهلا كلاس بسيط ومفيد في نفس الوقت

جزاك الله خير
الرد }}}
تم الشكر بواسطة: sendbad100


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  كود فتح واغلاق الفورم بطريقة جميلة 3booody 13 14,230 17-09-23, 07:30 PM
آخر رد: slimanedany
Lightbulb [كود] إغلاق الفورم عن طريق زر Esc و التعامل مع CancelButton Anas Mahmoud 1 2,528 18-06-21, 07:15 PM
آخر رد: AymanRamadanSaad
Star [VB.NET] ثيمات لتغيير شكل الفورم 3booody 0 2,606 17-03-21, 09:26 PM
آخر رد: 3booody
Lightbulb ايجاد عناصر الفورم من نوع معين ( PictureBox ) في List في سطر واحد Anas Mahmoud 0 1,664 22-09-20, 10:17 AM
آخر رد: Anas Mahmoud
  طريقتان لتصوير الفورم تختلف نتائجهما خصوصا بويندوز 10 سعود 2 2,392 17-06-20, 02:31 AM
آخر رد: سعود
  كود إضافة صورة في قاعدة بيانات أكسيس saffor 6 10,182 26-04-20, 11:08 PM
آخر رد: شوشه
  فتح الفورم DOX.1 1 2,624 04-03-20, 04:42 AM
آخر رد: محمد ايمن
  [كود] تحريك الكلام من عنوان الفورم و المربع+تحريك الكلمات في العنون و التكست محمد ايمن 2 2,435 04-03-20, 04:38 AM
آخر رد: محمد ايمن
  [VB.NET] اكواد التعامل مع الفورم : خلفية متدرجة للفورم بعدة طرق مختلفة علي بنيان 27 8,117 13-02-20, 01:00 PM
آخر رد: ابراهيم عمر ابراهيم
  كود بسيط لقرائة النصوص بالانجليزي فقط honest 7 5,084 22-10-19, 04:05 PM
آخر رد: sendbad100

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


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم