我想打印我添加到Winform应用程序中的WPF控件(MapControl)的可视内容。
(基本上,使用任何控件编译WPF用户控件,然后将结果控件像任何其他控件一样添加到Winform项目中。)
另一位用户基本上提供了一些代码来完成打印部分。看到: http://www.devexpress.com/Support/Center/Question/Details/Q386207
我可以在Winform方面收集的代码应该是:
private PrintDocument m_oPrintDoc;
public frmWhatever()
: base()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
// Set up the printing
m_oPrintDoc = new PrintDocument();
m_oPrintDoc.PrintPage += PrintDoc_PrintPage;
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = MapContainer;
}
问题是Winform端不存在PageVisual。它显然在WPF中。
等效代码是什么?我对“MapContainer”部分没有任何问题。唯一缺少的成分是“e.what”方法吗?
如果添加引用/使用对,是否可能存在PageVisual?
在有人要我联系DevExpress之前,我已经尝试过了。他们的回答是这个问题与他们没有任何关系,而且是一个纯粹的Microsoft.Net问题,因此问题就在这里。
哦,是的,我正在使用Visual Studio 2012和.Net 4.5。
答案 0 :(得分:1)
您正在使用GDI在WinForms中打印。
您的代码应该看起来像(未经测试):
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
var bmp = new Bitmap(e.MarginBounds);
MapContainer.DrawToBitmap(bmp, e.MarginBounds);
e.Graphics.DrawImage(bmp, new Point(0, 0));
}
如果显示某些内容,则必须调整缩放比例。可以很有趣(不是),使用PrintPreviewDialog来保存一些纸张。
答案 1 :(得分:1)
这是代码。第一个函数放在WPF控件中。我不得不建造一次。然后我在打印回调中获取了图像,我用它来设置图像。
public System.Drawing.Image GetMapImage()
{
RenderTargetBitmap rtb = new RenderTargetBitmap((int)this.mapControl1.ActualWidth, (int)this.mapControl1.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(this.mapControl1);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new System.IO.MemoryStream();
png.Save(stream);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
return image;
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
// Get the current map image. do not continue, if no image.
Image oMapResized = ((bvMaps.MapDevex)this.mapMain.Child).GetMapImage();
if (null == oMapResized)
return;
// Draw the image on the paper starting at the upper left corner.
e.Graphics.DrawImage((Image)oMapResized, new Point(0, 0));
}
我在StackOverflow上找到了另一篇帖子,这让我得到答案。该代码适用于任何WPF控件。
Get a bitmap image from a Control view
我确实必须在双方(Winforms和WPF)上添加一些引用,但在一天结束时,成功!