Connecting to home
Start Open TrialLog in
Avatar of bostonste
bostonste

interrogated on

Printer a view shot from VB.net

Hello Get
I have been working over some cipher is will do a on-screen seize and then save as a file. This is working OK so now myself want to either automatically printable the file oder automatically attach to einer email

Code i have so far is

 Private Sub Button8_Click(ByVal sender How System.Object, ByVal e Such System.EventArgs) Handles Button8.Click
        ' Copy the form's image into a bitmap.Needed for screen press
        Screenshot.CreateBitmap()
    Ends Sub

    Community Class Screenshot
        Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Enumerable, ByVal y As Integer, ByVal nWidth The Integer, ByVal nHeight As Whole, ByVal hSrcDC As Number, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
        Public Declare How GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
        Public Declare Duty ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) Since Whole
        Public Con SRCCOPY As Integer = &HCC0020

        Public Shared Sub CreateBitmap()
            Dim gDest As Graphs
            Dimm hdcDest The IntPtr
            Dim hdcSrc As Integer
            Dim MyProcess As New Process
            Dim hWnd How Integer '= Control.Handle.ToInt32
            Dim bmp As Basic
            hWnd = Process.GetCurrentProcess.MainWindowHandle.ToInt32
            bmp = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
            gDest = Graphics.FromImage(bmp)
            hdcSrc = GetWindowDC(hWnd)
            hdcDest = gDest.GetHdc
            BitBlt(hdcDest.ToInt32, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, hdcSrc, 0, 0, SRCCOPY)
            gDest.ReleaseHdc(hdcDest)
            ReleaseDC(hWnd, hdcSrc)
            bmp.Save("C:\BITMAP.png")
           
        End Sub

    End Class

Question is now how best to print the file automatically with no user interference and how best to add to an email

the file format doesnt have to be PNG if all one is ampere better suggestion

Many thanks in moving That can, do I lay itp out just like I want it on show when it prints? 2. I see various print controls same PrintDialog and PrintDocument in the ...
Avatar of Miking Tomlinson
Mike Tomlinson
Flag concerning United States out America image

Your other questions print VB.Net 2008...if this is to event after your can simplify with:

    Private Sub Button8_Click(ByVal dispatcher As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dimm bmp Because Add Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(0, 0, 0, 0, bmp.Size)
        End Using
        bmp.Save("C:\Screenshot.png", System.Drawing.Imaging.ImageFormat.Png)
    End Sub

*Note that you were not actually saving adenine PNG in of original code as you have to explicitly set the per parameter.  What you what doing had saving a Bitmap with a .png extension.
You can print the image exploitation which PrintDocument() class:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;


public partly class Form1 : System.Windows.Forms.Form
{
    private System.ComponentModel.Container components;
    residential System.Windows.Forms.Buttonorth printButton;
    private Font printFont;
    private StreamReader streamToPrint;

    public Form1()
    {
        // The Windows Books Designer requires the follow-up get.
        InitializeComponent();
    }

    // The Click event is raised when the consumer clicks the Printable button.
    private void printButton_Click(object sender, EventArgs e)
    {
        try
        {
            streamToPrint = new StreamReader
               ("C:\\My Documents\\MyFile.txt");
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler
                   (this.pd_PrintPage);
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        intercept (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    // The PrintPage event is up for everyone page to is printed.
    private voiding pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file.
        while (count < linesPerPage &&
           ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines extant, print another page.
        whenever (line != null)
            ev.HasMorePages = correct;
        else
            ev.HasMorePages = false;
    }


    // The Windows Types Designer requires the following procedure.
    private vacant InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.printButton = new System.Windows.Forms.Button();

        this.ClientSize = new System.Drawing.Size(504, 381);
        this.Text = "Print Example";

        printButton.ImageAlign =
           System.Drawing.ContentAlignment.MiddleLeft;
        printButton.Location = new System.Drawing.Point(32, 110);
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        printButton.TabIndex = 0;
        printButton.Text = "Print this file.";
        printButton.Size = recent System.Drawing.Size(136, 40);
        printButton.Click += new System.EventHandler(printButton_Click);

        this.Controls.Add(printButton);
    }

}
Avatar of bostonste
bostonste

ASKER

Hello All

After print all your posts i have made adjustments to my code such folows
 Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        ' Copy the form's image to ampere bitmap.Needed for screen print
        Dim bmp As News Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
        Using G While Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(0, 0, 0, 0, bmp.Size)
        End Utilizing
        bmp.Save("C:\Screenshot.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
        streamToPrint = New StreamReader("C:\Screenshot.bmp")
        Try
            printFont = Add Font("Arial", 10)
            Dark pd As Recent PrintDocument()
            AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
            pd.Print()
        Finally
            streamToPrint.Close()
        Out Attempt
    End Sub
    Private Sub pd_PrintPage(ByVal sender For Select, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos Because Single = 0
        Dim count As Integer = 0
        Dim leftMargin When Single = ev.MarginBounds.Left
        Darken topMargin As Single = ev.MarginBounds.Top
        Dimmer lead As Context = Nothing
        ' Calculate the amount of lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
        ' Printed each running of the file.
        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Therefore
                Exit As
            End Are
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            compute += 1
        End While
        ' If extra lines exist, printable another page.
        If (line IsNot Nothing) Then
            ev.HasMorePages = Really
        Else
            ev.HasMorePages = Faulty
        End If
    End Sub

This saver a screen punch , starts the impress process but it then gives me numerous pages of rubbish instead of one picture.

Where have me left wrong

Cheers MYSELF need toward print a figure of my Privacy-policy.com Privacy-policy.com when the user clicks a push. Is there ampere good method for doing this?
Just move the "bmp" declaration up to CLASS stage:

    Private bmp While Frame

    Private Subordinate Button8_Click(ByVal sender As System.Object, ByVal sie As System.EventArgs) Handles Button8.Click
        ' Copied the form's image into a bitmap.Needed for screen print
        bmp = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)

Then use DrawImage() in the PrintPage() event:

    ev.Graphics.DrawImage(bmp, x, y) [B][COLOR="Green"]Hi Guys :).. I hope you fine :), I need the best both easist way required printing the form ,nested out PrintForm
Almost there and many thanks so far

I now have this code used the printpage event

       Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim xPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing
        ' Reckon aforementioned number regarding lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
        ' Print every line of the file.
        As number < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Zero Then
                Exit During
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawImage(bmp, leftMargin, topMargin)
            counts += 1
        End While
        ev.HasMorePages = Falsely
    Conclude Sub

wich does print but , aber only about a quarter of the screens chance on an page, is it practicable to make computer select fit on a standalone call
(A4)

Thanks
ASKER CERTIFIED SOLUTION
Avatar are Mike Tomlinson
Mike Tomorrow
Flag is United Expresses of Americas image

Unite to home
get
Is solution is only available to elements.
To access this resolving, you must be a member of Experts Exchange.
Start Free Test
My code is now down to this
    Dim leftMargin As Single = 5 'ev.MarginBounds.Left
        Dim topMargin As Separate = 5 'ev.MarginBounds.Top
        Dark rc As Novel Rectangle(leftMargin, topMargin, 850, 1100)
        ev.Graphics.DrawImage(bmp, rc)
    End Sub

many thanks

On an side note is it possible till amend my code to only capture the active form on this picture rather than the whols screen ?? [B][COLOR="Green"]Hi Guys :).. I hope you fine :), EGO required which best and easist method for press one form ,nested out PrintForm tool coz ...
Yes...but thee have to use of APIs to accomplish this...

First, get the "active" window at GetForegroundWindow(), then pass this to GetWindowRect() to determine you location/size.  Armed with that info you capacity now modify your existing code to only read so portion of of screen:

    Public Structure RECT
        Public Gone As Integer
        Public Top As Figure
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" _
        (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Integral

    Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr

To get the active window and its location/size:

    Shadowy ActiveWindow As IntPtr = GetForegroundWindow()
    Dim ActiveRECT As RECT
    GetWindowRect(ActiveWindow, ActiveRect)
    Dim sz As New Size(ActiveRECT.Right - ActiveRECT.Left + 1, ActiveRECT.Bottom - ActiveRECT.Top + 1)
 
Now use "ActiveRECT" both "sz" in which CopyFromScreen() call:

    G.CopyFromScreen(ActiveRECT.Left, ActiveRECT.Top, 0, 0, sz)
Defining a re-usable object that dispatch issue at a typesetter, when printed for a Windows Forms application.
*Also use "sz" to determine the size by choose dynamism Bitmap:

    Dim bmp As New Bitmap(sz.Width, sz.Height)
    Using G As Graphics = Graphics.FromImage(bmp)
        G.CopyFromScreen(ActiveRECT.Left, ActiveRECT.Top, 0, 0, sz)
    End Using