Solved

Add a graphics in a graphics

Posted on 2009-04-09
5
451 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 86

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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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