【问题标题】:printing scrollable panel with image inside in vb在vb中打印带有图像的可滚动面板
【发布时间】:2015-06-04 11:09:49
【问题描述】:

大家好。我在可滚动面板中打印图像时遇到问题。谁能帮我解决这个问题?

我想先调整图像的大小(这样它可以适合 8.5 x 11" 的纸张),然后在打印预览中显示然后打印

在我的例子中,我有一个带有面板(dock=fill)的表单(form1),我想在其中打印图像(2 个图像)。但是,图像打印的触发器在下一个表单中

在 btnnext(form1) 中 Form2.Show() 我.Hide()

打印代码为form2。我是 vb 的新手,我对打印可滚动面板一无所知。我在其他论坛尝试了不同的代码,但它没有打印整个图像,而只打印了表单的屏幕截图。任何回复将不胜感激。,非常感谢您


Public Class Form2

Private WithEvents pd As New Printing.PrintDocument
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Using bmp As New Bitmap(Form1.pb1.Width, Form1.pb1.Height)
        Form1.panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Form1.pb1.Width, Form1.pb1.Height))
        e.Graphics.DrawImage(bmp, e.MarginBounds)
    End Using
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    PrintDocument1.Print()
End Sub 
End Class

- ^它只打印您在屏幕上可以看到的内容。不是整个面板..

【问题讨论】:

  • 你在用panel.DrawToBitmap()吗?
  • 使用 bmp 作为新位图(Form1.pb1.Width, Form1.pb1.Height) Form1.pb1.DrawToBitmap(bmp, New Rectangle(0, 0, Form1.pb1.Width, Form1.pb1 .Height)) e.Graphics.DrawImage(bmp, e.MarginBounds) End Using(我使用了这个代码并且它正在工作)现在唯一的问题是打印预览......我不知道打印预览......
  • DrawToBitmap 总是截取面板的屏幕截图,因此只获取可见的内容。这个问题之前在这里被问过:stackoverflow.com/questions/22056099/… 但是它从来没有得到回答
  • 哎呀。我还是有问题。我有 2 张图片,所以使用 form1.pb1.drawtobitmap 将不起作用。我使用了 Form1.panel1.DrawToBitmap 但它只打印您在表单中看到的内容(而不是面板中的整个图像)
  • 您始终可以在PrintPage 事件中生成两个不同的图像并将它们打印在不同的页面上

标签: vb.net printing panel scrollable


【解决方案1】:

基本上,保持所有内容相同,但不是在整个页面上打印整个第一张图像,而是根据其位置和大小相对于 MarginBounds 的比例打印第二张图像

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Using bmp As New Bitmap(Form1.pb1.Width, Form1.pb1.Height)
        Form1.pb1.DrawToBitmap(bmp, New Rectangle(0, 0, Form1.pb1.Width, Form1.pb1.Height))
        'Print pb1 to fill the whole page
        e.Graphics.DrawImage(bmp, e.MarginBounds)
    End Using
    Using bmp2 As New Bitmap(Form1.pb2.Width, Form1.pb2.Height)
        Form1.pb2.DrawToBitmap(bmp2, New Rectangle(0, 0, Form1.pb2.Width, Form1.pb2.Height))

        Dim x as Double = Form1.pb2.Location.X - Form1.pb1.Location.X
        x = x * e.MarginBounds.Width / Form1.pb1.Width
        x = x + e.MarginBounds.X

        Dim y as Double = Form1.pb2.Location.Y - Form1.pb1.Location.Y
        y = y * e.MarginBounds.Height / Form1.pb1.Height
        y = y + e.MarginBounds.Y

        Dim width as Double = Form1.pb2.width
        width = width * e.MarginBounds.Width / Form1.pb1.Width

        Dim height as Double = Form1.pb2.Height
        height = height * e.MarginBounds.Height / Form1.pb1.Height

        e.Graphics.DrawImage(bmp2, New Rectangle(x, y, width, height))
    End Using
End Sub

【讨论】:

  • 但我没有投反对票?无论如何,它显示了第二张图像,但问题是它显示了图像的实际大小(没有找到第一张图像的右侧部分,或者它没有显示面板中显示的内容),而第一个被拉伸.
  • 不,我知道不是你 :) 我指的是投反对票的人...无论如何,尝试使用两条线e.Graphics.DrawImage,因为它们控制着两者的比例图片。我已经设置了:第一张图片:从左上角开始到页面的一半,第二张从页面的一半到最右边
  • 它实际上在页面左侧打印了第一个,在页面右侧打印了第二个。你是对的。但我的问题是,我想打印可滚动面板中显示的内容。因为箭头/定位器(第二张图片)很重要。它需要在正确的位置
  • 你能不能发个截图什么的,因为我不确定我是否理解你想要的样子
  • 我无法发布图片 :( 但它看起来像这样> encrypted-tbn1.gstatic.com/… (但带有滚动条并想象第一张图片是地图,第二张是黑色的)抱歉迟到的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
相关资源
最近更新 更多