?
Solved

Add a graphics in a graphics

Posted on 2009-04-09
5
Medium Priority
?
457 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
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 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