Metafile copy does not display zoom correctly
Posted: Tue May 03, 2005 6:58 am
Hi!
Have used the code below to copy the chart to the clipboard.
However if the chart is zoomed in the y-axis the copy comes out with huge margins above and under the chart. Since I need to paste the copy correctly into a graphics object together with other stuff i would like a way to copy only the chart without margins also when it is zoomed in. (This works fine in x-axis - the copied chart does not have margins to the left or to the right.)
How can i get rid of the margins?
Best regards Elisabeth
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WinCsharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private int clickedX;
private int clickedY;
private string msgText;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private Steema.TeeChart.TChart tChart1;
private Steema.TeeChart.Styles.Line line1;
[DllImport("user32.dll")]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
static extern bool EmptyClipboard();
[DllImport("user32.dll")]
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
static extern bool CloseClipboard();
[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
[DllImport("gdi32.dll")]
static extern bool DeleteEnhMetaFile(IntPtr hemf);
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#region Windows Form Designer generated code
private void Form1_Load(object sender, System.EventArgs e) {
line1.FillSampleValues(20);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Drawing.Imaging.Metafile mf;
Graphics g = this.CreateGraphics();
IntPtr hdc = g.GetHdc();
mf = new System.Drawing.Imaging.Metafile(stream, hdc);
g.ReleaseHdc(hdc);
g.Dispose();
g = Graphics.FromImage(mf);
tChart1.Draw(g);
g.Dispose();
PutEnhMetafileOnClipboard(this.Handle, mf);
}
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;
}
}
}
Have used the code below to copy the chart to the clipboard.
However if the chart is zoomed in the y-axis the copy comes out with huge margins above and under the chart. Since I need to paste the copy correctly into a graphics object together with other stuff i would like a way to copy only the chart without margins also when it is zoomed in. (This works fine in x-axis - the copied chart does not have margins to the left or to the right.)
How can i get rid of the margins?
Best regards Elisabeth
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WinCsharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private int clickedX;
private int clickedY;
private string msgText;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private Steema.TeeChart.TChart tChart1;
private Steema.TeeChart.Styles.Line line1;
[DllImport("user32.dll")]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
static extern bool EmptyClipboard();
[DllImport("user32.dll")]
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
static extern bool CloseClipboard();
[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
[DllImport("gdi32.dll")]
static extern bool DeleteEnhMetaFile(IntPtr hemf);
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#region Windows Form Designer generated code
private void Form1_Load(object sender, System.EventArgs e) {
line1.FillSampleValues(20);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Drawing.Imaging.Metafile mf;
Graphics g = this.CreateGraphics();
IntPtr hdc = g.GetHdc();
mf = new System.Drawing.Imaging.Metafile(stream, hdc);
g.ReleaseHdc(hdc);
g.Dispose();
g = Graphics.FromImage(mf);
tChart1.Draw(g);
g.Dispose();
PutEnhMetafileOnClipboard(this.Handle, mf);
}
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;
}
}
}