protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);

            Stream pageToPrint = m_pages[m_currentPage];
            pageToPrint.Position = 0;

            // Load each page into a Metafile to draw it.
            using (Metafile pageMetaFile = new Metafile(pageToPrint))
            {
                Rectangle adjustedRect = new Rectangle(
                        e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                        e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                        e.PageBounds.Width,
                        e.PageBounds.Height);

                // Draw a white background for the report
                e.Graphics.FillRectangle(Brushes.White, adjustedRect);

                // Draw the report content
                e.Graphics.DrawImage(pageMetaFile, adjustedRect);

                // Prepare for next page.  Make sure we haven't hit the end.
                m_currentPage++;
                e.HasMorePages = m_currentPage < m_pages.Count;
            }
        }
    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        if (this.PrinterSettings.PrintRange == System.Drawing.Printing.PrintRange.SomePages)
        {
            while (++_page < this.PrinterSettings.FromPage)
            {
                // Fake the pages that need to be skipped by printing them to a Metafile
                IntPtr hDev = e.Graphics.GetHdc();
                try
                {
                    using (var mf = new Metafile(hDev, e.PageBounds))
                    using (var gr = Graphics.FromImage(mf))
                    {
                        var ppe = new PrintPageEventArgs(gr, e.MarginBounds, e.PageBounds, e.PageSettings);
                        base.OnPrintPage(ppe);
                    }
                }
                finally
                {
                    e.Graphics.ReleaseHdc(hDev);
                }
            }

            // Print the real page
            base.OnPrintPage(e);

            // No need to continue past PageTo
            if (this.PrinterSettings.ToPage > 0 && _page >= this.PrinterSettings.ToPage) e.HasMorePages = false;
        }
        else
        {
            base.OnPrintPage(e);
        }
    }
 public static Bitmap GetImage(IntPtr hwnd)
 {
     try
         {
             if (OpenClipboard(hwnd))
             {
                 IntPtr data = GetClipboardData(14); // CF_ENHMETAFILE      14
                 if (data != IntPtr.Zero)
                 {
                     using (Metafile mf = new Metafile(data, true))
                     {
                         Bitmap b = new Bitmap(mf);
                         return b;
                     }
                 }
             }
         }
         finally
         {
             CloseClipboard();
         }
         return null;
 }
Exemple #4
0
	// Metafile mf is set to a state that is not valid inside this function.
	static public bool PutEnhMetafileOnClipboard( IntPtr hWnd, Metafile mf )
	{
		bool bResult = false;
		IntPtr hEMF, hEMF2;
		hEMF = mf.GetHenhmetafile(); // invalidates mf
		if( ! hEMF.Equals( new IntPtr(0) ) )
		{
			hEMF2 = CopyEnhMetaFile( hEMF, new IntPtr(0) );
			if( ! hEMF2.Equals( new IntPtr(0) ) )
			{
				if( OpenClipboard( hWnd ) )
				{
					if( EmptyClipboard() )
					{
						IntPtr hRes = SetClipboardData( 14 /*CF_ENHMETAFILE*/, hEMF2 );
						bResult = hRes.Equals( hEMF2 );
						CloseClipboard();
					}
				}
			}
			DeleteEnhMetaFile( hEMF );
		}
		return bResult;
	}
Exemple #5
0
    private void saveEmf(String path)
    {
        FileStream fs = new FileStream(path,FileMode.Create );
        Graphics g = CreateGraphics();
        IntPtr hDC = g.GetHdc();
        Metafile m = new Metafile( fs, hDC );

        g.ReleaseHdc( hDC );
        g.Dispose();

        Graphics vg= Graphics.FromImage(m);
        Node.drawComponent(currentSymbol, vg);
        vg.Dispose();
        m.Dispose();
        fs.Close();
    }
		protected virtual void ExportToMetafile(FileStream stream, string path)
		{
			Metafile metafile = new Metafile();
			metafile.AddDiagram(this);

			if (stream != null)
			{
				metafile.Save(stream);
			}
			else
			{
				metafile.Save(path);
			}
		}
Exemple #7
0
    /// <summary>
    /// Wraps the image in an Enhanced Metafile by drawing the image onto the
    /// graphics context, then converts the Enhanced Metafile to a Windows
    /// Metafile, and finally appends the bits of the Windows Metafile in HEX
    /// to a string and returns the string.
    /// </summary>
    /// <param name="_image"></param>
    /// <returns>
    /// A string containing the bits of a Windows Metafile in HEX
    /// </returns>
    private string GetRtfImage(Image _image)
    {
        StringBuilder _rtf = null;

        // Used to store the enhanced metafile
        MemoryStream _stream = null;

        // Used to create the metafile and draw the image
        Graphics _graphics = null;

        // The enhanced metafile
        Metafile _metaFile = null;

        // Handle to the device context used to create the metafile
        IntPtr _hdc = default(IntPtr);

        try
        {
            _rtf = new StringBuilder();
            _stream = new MemoryStream();

            // Get a graphics context from the RichTextBox
            using (var __graphics = this.CreateGraphics())
            {

                // Get the device context from the graphics context
                _hdc = __graphics.GetHdc();

                // Create a new Enhanced Metafile from the device context
                _metaFile = new Metafile(_stream, _hdc);

                // Release the device context
                __graphics.ReleaseHdc(_hdc);
            }

            // Get a graphics context from the Enhanced Metafile
            using (var __graphics = Graphics.FromImage(_metaFile))
            {

                // Draw the image on the Enhanced Metafile

                __graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height));
            }

            // Get the handle of the Enhanced Metafile
            IntPtr _hEmf = _metaFile.GetHenhmetafile();

            // A call to EmfToWmfBits with a null buffer return the size of the
            // buffer need to store the WMF bits.  Use this to get the buffer
            // size.
            uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

            // Create an array to hold the bits
            byte[] _buffer = new byte[_bufferSize];

            // A call to EmfToWmfBits with a valid buffer copies the bits into the
            // buffer an returns the number of bits in the WMF.
            uint _convertedSize = GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);

            // Append the bits to the RTF string
            for (int i = 0; i <= _buffer.Length - 1; i++)
            {
                _rtf.Append(String.Format("{0:X2}", _buffer[i]));
            }

            return _rtf.ToString();
        }
        finally
        {
            if (_graphics != null)
            {
                _graphics.Dispose();
            }
            if (_metaFile != null)
            {
                _metaFile.Dispose();
            }
            if (_stream != null)
            {
                _stream.Close();
            }
        }
    }
Exemple #8
0
    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
        ev.Graphics.DrawImage(pageImage, ev.PageBounds);

        m_currentPageIndex++;
        ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
    }