Solved

Add a graphics in a graphics

Posted on 2009-04-09
5
447 Views
Last Modified: 2013-11-07
Hello
i try to create a new System.Drawing.Printing.PrintDocument with jump page, but i have some problem.

i have many System.Drawing.Graphics and i try to add all in e.graphics of type System.Drawing.Graphics.
Example: e.Graphics.DrawGraphics(myGraphics,0,0)

but DrawGraphics doesn't exist :-(

i read on the internet, many examples of that and every body pass by bitmap and use printImage. But every time i try, that create a blank bitmap or black bitmap

somebody can help me ???
Private Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As IntPtr, ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal dwRop As Int32) As Int32

    Private Const SRCCOPY As Int32 = &HCC0020

    Public Function GetPictureOfGraphics(ByVal Width As Integer, ByVal Height As Integer, ByRef srcPic As Graphics) As Bitmap

        Dim srcBmp As New Bitmap(Width, Height, srcPic)

        Dim copyRect As Bitmap

        'Create a Graphics object in memory from that bitmap

        Dim srcMem As Graphics = Graphics.FromImage(srcBmp)

        'get the IntPtr's of the graphics

        Dim HDC1 As IntPtr = srcPic.GetHdc

        'get the IntPtr's of the graphics

        Dim HDC2 As IntPtr = srcMem.GetHdc

        'get the picture

        BitBlt(HDC2, 0, 0, Width, Height, HDC1, 0, 0, SRCCOPY)

        'Clean Up

        copyRect = srcBmp.Clone()

        srcPic.ReleaseHdc(HDC1)

        srcMem.ReleaseHdc(HDC2)

        srcPic.Dispose()

        srcMem.Dispose()

        Return copyRect

    End Function
 

Private Sub PrintDocumentSofticket_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage

        Dim font As New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

        For i As Integer = 1 To 20

            e.Graphics.DrawString("Paneau " & i, font, Brushes.Black, 0, 0)

            e.Graphics.DrawImage(Image.FromFile("C:\programmation2\Programmation\Softicket Develop\Build\Images\Softicket_Grand_Logo copie1.bmp"), 5, 16)

        Next i

        GetPictureOfGraphics(Me.PrinterSettings.DefaultPageSettings.PaperSize.Width, Me.PrinterSettings.DefaultPageSettings.PaperSize.Height, e.Graphics).Save("C:/testprint.png")

    End Sub

Open in new window

0
Comment
Question by:Waxime
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 24109548
Perhaps you could try something more like this:

    Private Sub PrintDocumentSofticket_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage
        Dim img As Image = Nothing
        Dim imgName As String = "C:\programmation2\Programmation\Softicket Develop\Build\Images\Softicket_Grand_Logo copie1.bmp"
        Using fs As New System.IO.FileStream(imgName, IO.FileMode.Open)
            img = Image.FromStream(fs)
        End Using

        Dim bmp As New Bitmap(Me.PrinterSettings.DefaultPageSettings.PaperSize.Width, Me.PrinterSettings.DefaultPageSettings.PaperSize.Height, e.Graphics)
        Using font As New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Using bmpG As Graphics = Graphics.FromImage(bmp)
                For i As Integer = 1 To 20
                    Draw(e.Graphics, font, i, img)
                    Draw(bmpG, font, i, img)
                Next i
            End Using
        End Using
        bmp.Save("C:\testprint.png", System.Drawing.Imaging.ImageFormat.Png)
    End Sub

    Private Sub Draw(ByVal g As Graphics, ByVal fnt As Font, ByVal i As Integer, ByVal img As Image)
        g.DrawString("Paneau " & i, fnt, Brushes.Black, 0, 0)
        g.DrawImage(img, 5, 16)
    End Sub

Though I don't understand why you are drawing "Paneua1" thru "Paneua20" using the same font in the exact same place.

The concept should help you though...we call a generic draw routine that simply accepts a Graphics to draw on.  It doesn't know if the graphics came from the Print process or from the Bitmap...
0
 

Author Comment

by:Waxime
ID: 24110376
thanks for the response
yes is true, all panneau (in english is panel) is in the same place, in this example, but in the future, all is in different graphics

but that is a stupid example, but all Graphics i want to merge is create by a XML file. But for easer example, i bypass everything and i try with only a picture and a string

the final result is more same of that, in the final and i really have to merge 2 graphics objects
Protected Sections As New GenericSofticket.genericListSofticket(Of GraphicsSofticket)
 

Private positionPrint As Integer = 0
 

Private Sub PrintDocumentSofticket_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

   positionPrint = 0

End Sub
 

Private Sub PrintDocumentSofticket_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage

        With Sections.getEnumerator

            .Jump(positionPrint)

            Dim top As Single = 0

            While .MoveNext

                If top + .Value.getHeight >= Me.PrinterSettings.DefaultPageSettings.PaperSize.Height Then

                    e.HasMorePages = True

                    Exit Sub

                Else

e.Graphics.DrawImage(GetPictureOfGraphics(Me.PrinterSettings.DefaultPageSettings.PaperSize.Width, .Value.getHeight, .Value.getGraphics), 0, top)

                    top += .Value.getHeight

                    positionPrint += 1

                End If

            End While

        End With

    End Sub

Open in new window

0
 

Author Comment

by:Waxime
ID: 24110547
i found the solution, but not totally
i don't understand copying graphics in graphics not work, but went i create my panel, i conserve the bitmap file
    Public Class GraphicsSofticket

        Private Graphics As Graphics

        Private height As Integer = 0

        Private bmp As Bitmap
 

        Public Sub New(ByVal PrintDocumentSofticket As PrintDocumentSofticket)

            bmp = New Bitmap(PrintDocumentSofticket.DefaultPageSettings.PaperSize.Width, PrintDocumentSofticket.DefaultPageSettings.PaperSize.Height)

            Me.Graphics = Graphics.FromImage(bmp)
 

        End Sub

       Public Function getHeight() As Integer

            Return Me.height

        End Function

        Public Function getGraphics() As Graphics

            Return Me.Graphics

        End Function

        Public Function getBmp() As Bitmap

            Return Me.bmp

        End Function

....
 
 

e.Graphics.DrawImage(.Value.getBmp, 0, top, Me.PrinterSettings.DefaultPageSettings.PaperSize.Width, .Value.getHeight)

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24111328
That's basically what I showed you already!...   =\

I created a Bitmap and got a Graphics to it:

    Dim bmp As New Bitmap(Me.PrinterSettings.DefaultPageSettings.PaperSize.Width, Me.PrinterSettings.DefaultPageSettings.PaperSize.Height, e.Graphics)
    ...
            Using bmpG As Graphics = Graphics.FromImage(bmp)

You created a Bitmap and got a Graphics to it:

        Private Graphics As Graphics
        Private height As Integer = 0
        Private bmp As Bitmap
 
        Public Sub New(ByVal PrintDocumentSofticket As PrintDocumentSofticket)
            bmp = New Bitmap(PrintDocumentSofticket.DefaultPageSettings.PaperSize.Width, PrintDocumentSofticket.DefaultPageSettings.PaperSize.Height)
            Me.Graphics = Graphics.FromImage(bmp)
        End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now