[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Add a graphics in a graphics

Posted on 2009-04-09
5
Medium Priority
?
455 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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