?
Solved

Add a graphics in a graphics

Posted on 2009-04-09
5
Medium Priority
?
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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