Public Class Form_barcode
    Dim PrintDoc As Printing.PrintDocument = New Printing.PrintDocument()
    Dim pd_PrintDialog As New PrintDialog
    ' Dim MyImage As System.Drawing.Image = PicBarCode.BackgroundImage
    ' تم ايقاف هذا الاجراء و لم يعد يستخدم و ترك مؤقتا 
    Public Sub PrintBarCodeV()

        pd_PrintDialog.UseEXDialog = True
        pd_PrintDialog.AllowPrintToFile = False

        'تحديد عدد النسخ المراد طباعتها
        pd_PrintDialog.PrinterSettings.Copies = nud_Count.Value

        'عرض صندوق حوار اختيار الطابعة
        If pd_PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDoc.PrinterSettings = pd_PrintDialog.PrinterSettings
            AddHandler PrintDoc.PrintPage, AddressOf PrintDocHandler

        End If

    End Sub
    'This is the event handler for printing bar codes
    Private Sub PrintDocHandler(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
        ev.Graphics.DrawImage(PicBarCode.BackgroundImage, nud_MarginH.Value, nud_MarginW.Value, nud_MarginWBrcode.Value, nud_MarginHBrcode.Value)
    End Sub


    Private Sub Texbarc_TextChanged(sender As Object, e As EventArgs) Handles Texbarc.TextChanged
        PicBarCode.BackgroundImage = Code128(Texbarc.Text, "A")
    End Sub

    Private Sub Butred_Click(sender As Object, e As EventArgs) Handles Butred.Click
        'كتابة اسم المنتج على ملصق الباركود
        Dim a As Graphics = PicBarCode.CreateGraphics
        a.DrawString(Textreding.Text, Me.Font, Brushes.Black, 100, 5)

        'Me.PicBarCode.Image = PicBarCode.BackgroundImage



        'Dim Bmp As New Bitmap(PicBarCode.Image)
        'Dim Graphics1 As Graphics = Graphics.FromImage(Bmp)
        'Dim Font1 As Font = New Font("Comic Sans MS", 20, FontStyle.Bold, GraphicsUnit.Pixel)
        'Dim Brush1 As New SolidBrush(Color.FromArgb(92, 230, 80, 120))
        'Graphics1.DrawString(TextBox1.Text, Me.Font, Brushes.Black, 100, 5)
        'PicBarCode.Image = Bmp
    End Sub

    Private Sub Buprave_Click(sender As Object, e As EventArgs) Handles Buprave.Click
        'معاينة قبل الطباعة
        If Texbarc.Text = "" Then 'الباركود فارغ 
            MsgBox("ادخل القيمة", MsgBoxStyle.Critical, "تنبيه")
            Exit Sub
        Else
            AddHandler PrintDoc.PrintPage, AddressOf PrintDocHandler

            'تحديد عدد النسخ المراد طباعتها
            pd_PrintDialog.PrinterSettings.Copies = nud_Count.Value

            PrintPreviewDialog1.Document = PrintDoc
            PrintPreviewDialog1.ShowDialog()

        End If
    End Sub

    Private Sub Buttprintt_Click(sender As Object, e As EventArgs) Handles Buttprintt.Click
        Try

     
        'طباعة الباركود
        If Texbarc.Text = "" Then 'الباركود فارغ 
            MsgBox("ادخل القيمة", MsgBoxStyle.Critical, "تنبيه")
            Exit Sub
        Else

            pd_PrintDialog.UseEXDialog = True
            pd_PrintDialog.AllowPrintToFile = False
            'تحديد عدد النسخ المراد طباعتها.
            pd_PrintDialog.PrinterSettings.Copies = nud_Count.Value
            If pd_PrintDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

                PrintDoc.PrinterSettings = pd_PrintDialog.PrinterSettings


                    AddHandler PrintDoc.PrintPage, AddressOf PrintDocHandler

                'امر الطباعة
                PrintDoc.Print()

            ElseIf pd_PrintDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then
                Exit Sub

            End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "تنبيه")
        End Try
    End Sub

    Private Sub Butsaver_Click(ByVal sender As Object, ByVal e As EventArgs)
       
    End Sub

    Private Sub butPageSetup_Click(ByVal sender As Object, ByVal e As EventArgs) Handles butPageSetup.Click
        'اعداد الصفحة

        PageSetupDialog1.Document = PrintDoc
        PageSetupDialog1.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'حفظ صورة الباركود
        If Texbarc.Text = "" Then 'الباركود فارغ 
            MsgBox("من فضلك تأكد من كتابة النص المراد تحويله الى باركود")
            Exit Sub
        Else
            Dim SaveFileDialog1 As New SaveFileDialog()
            SaveFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) ' "c:\" 
            SaveFileDialog1.Filter = "All files (*.*)|*.*|Bitmap files (*.bmp)|*.bmp"
            SaveFileDialog1.FilterIndex = 2
            SaveFileDialog1.RestoreDirectory = True
            SaveFileDialog1.ShowDialog()
            If SaveFileDialog1.FileName <> "" Then
                PicBarCode.BackgroundImage.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)

            End If
        End If

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub PicBarCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PicBarCode.Click

    End Sub
End Class