c#如何在多页中打印出一个图像框的数组

发布于 2025-02-01 22:02:25 字数 1187 浏览 4 评论 0原文

使用C#,我编写了以下代码,以在picturebox pictureboxarr []的数组(size = TotalToprint)中打印出图像,每个代码的大小为100x100像素。我想垂直打印它们,它们之间的20像素距离高度distanceBetemages Imimages的问题是,它仅在1页上打印(例如字母尺寸),无论它只有多少张图像(然后仅打印8张图像并删除其余图像)。如何解决此问题并在多页上打印?

    int totalToPrint;
    int xFirstAncorPoint = 100;
    int yFirstAncorPoint = 100;
    int ImagSize = 100; // Squre of 100x100 pixel 
    int heightDistanceBetweenImages = 20;



  PrintDialog pd = new PrintDialog();
        PrintDocument pDoc = new PrintDocument();
        pDoc.PrintPage += PrintPicture;
        pd.Document = pDoc;
        if (pd.ShowDialog() == DialogResult.OK)
        {
            pDoc.Print();
        }

    }


    public void PrintPicture(Object sender, PrintPageEventArgs e)
    {
        Bitmap bmp1 = new Bitmap(ImagSize , totalToPrint * (ImagSize + heightDistanceBetweenImages));
        for (int i = 0; i < totalToPrint; i++)
        {
            pictureBoxArr[i].DrawToBitmap(bmp1, new Rectangle(0, i * (heightDistanceBetweenImages + ImagSize), pictureBoxArr[0].Width, pictureBoxArr[0].Height));
        }
        e.Graphics.DrawImage(bmp1, xFirstAncorPoint, yFirstAncorPoint);
        bmp1.Dispose();
    }

using C#, I wrote the below code to print out images in an array (size = totalToPrint) of Picturebox pictureBoxArr[] each in size of 100x100 pixel. I want to print them vertically with the 20-pixel distance between them heightDistanceBetweenImages the problem is that it only prints on 1 page (say letter size) no matter how many images ( then it only prints 8 images and dismisses the rest). How can I solve this problem, and print it on multiple pages?

    int totalToPrint;
    int xFirstAncorPoint = 100;
    int yFirstAncorPoint = 100;
    int ImagSize = 100; // Squre of 100x100 pixel 
    int heightDistanceBetweenImages = 20;



  PrintDialog pd = new PrintDialog();
        PrintDocument pDoc = new PrintDocument();
        pDoc.PrintPage += PrintPicture;
        pd.Document = pDoc;
        if (pd.ShowDialog() == DialogResult.OK)
        {
            pDoc.Print();
        }

    }


    public void PrintPicture(Object sender, PrintPageEventArgs e)
    {
        Bitmap bmp1 = new Bitmap(ImagSize , totalToPrint * (ImagSize + heightDistanceBetweenImages));
        for (int i = 0; i < totalToPrint; i++)
        {
            pictureBoxArr[i].DrawToBitmap(bmp1, new Rectangle(0, i * (heightDistanceBetweenImages + ImagSize), pictureBoxArr[0].Width, pictureBoxArr[0].Height));
        }
        e.Graphics.DrawImage(bmp1, xFirstAncorPoint, yFirstAncorPoint);
        bmp1.Dispose();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2025-02-08 22:02:25

你大约在那里。 将是您的未来参考。

printPage不仅为您提供了绘图空间;它还具有您的页面范围,页面边距等。如果您需要打印更多页面,它还设置了HASMOREPAGES属性。此属性默认为false,因此您仅打印1页。另外,如果在页面的边界之外,它不会打印出来。随着这里和那里的一点变化,您最终会得到类似的东西。

// using queue to manage images to print
Queue<Image> printImages = new Queue<Image>();
int totalToPrint;
int xFirstAncorPoint = 100;
int yFirstAncorPoint = 100;
int ImagSize = 100; // Squre of 100x100 pixel 
int heightDistanceBetweenImages = 20;

private void btnPrintTest_Click(object sender, EventArgs e) {
    PrintDialog pd = new PrintDialog();
    PrintDocument pDoc = new PrintDocument();
    pDoc.PrintPage += PrintPicture;
    pd.Document = pDoc;
    if (pd.ShowDialog() == DialogResult.OK) {
        // add image references to printImages queue.
        for (int i = 0; i < pictureBoxArr.Length; i++) {
            printImages.Enqueue(pictureBoxArr[i].Image);
        }
        pDoc.Print();
    }
}

private void PrintPicture(object sender, PrintPageEventArgs e) {
    int boundsHeight = e.MarginBounds.Height;       // Get height of bounds that we are expected to print in.
    int currentHeight = yFirstAncorPoint;

    while (currentHeight <= boundsHeight && printImages.Count > 0) {
        var nextImg = printImages.Peek();
        int nextElementHeight = nextImg.Height + heightDistanceBetweenImages;

        if (nextElementHeight + currentHeight <= boundsHeight) {
            e.Graphics.DrawImage(nextImg, new PointF(xFirstAncorPoint, currentHeight + heightDistanceBetweenImages));

            printImages.Dequeue();
        }

        currentHeight += nextElementHeight;
    }

    // how we specify if we may have more pages to print
    e.HasMorePages = printImages.Count > 0;
}

希望这能使您走上正确的道路,并为您的代码进行一些较小的调整,您将拥有所需的东西。

You are about half way there. PrintDocument would be your future reference.

The PrintPage gives you more than just a drawing space; it also has your page bounds, page margins, etc. It also has HasMorePages property that you set if you need to print more pages. This property defaults to false, so you were only printing 1 page. Also if anything is outside the bounds of the page, it would not print that. With a little change here and there, you would end up with something like this.

// using queue to manage images to print
Queue<Image> printImages = new Queue<Image>();
int totalToPrint;
int xFirstAncorPoint = 100;
int yFirstAncorPoint = 100;
int ImagSize = 100; // Squre of 100x100 pixel 
int heightDistanceBetweenImages = 20;

private void btnPrintTest_Click(object sender, EventArgs e) {
    PrintDialog pd = new PrintDialog();
    PrintDocument pDoc = new PrintDocument();
    pDoc.PrintPage += PrintPicture;
    pd.Document = pDoc;
    if (pd.ShowDialog() == DialogResult.OK) {
        // add image references to printImages queue.
        for (int i = 0; i < pictureBoxArr.Length; i++) {
            printImages.Enqueue(pictureBoxArr[i].Image);
        }
        pDoc.Print();
    }
}

private void PrintPicture(object sender, PrintPageEventArgs e) {
    int boundsHeight = e.MarginBounds.Height;       // Get height of bounds that we are expected to print in.
    int currentHeight = yFirstAncorPoint;

    while (currentHeight <= boundsHeight && printImages.Count > 0) {
        var nextImg = printImages.Peek();
        int nextElementHeight = nextImg.Height + heightDistanceBetweenImages;

        if (nextElementHeight + currentHeight <= boundsHeight) {
            e.Graphics.DrawImage(nextImg, new PointF(xFirstAncorPoint, currentHeight + heightDistanceBetweenImages));

            printImages.Dequeue();
        }

        currentHeight += nextElementHeight;
    }

    // how we specify if we may have more pages to print
    e.HasMorePages = printImages.Count > 0;
}

Hopefully this gets you on the right path and with some minor tweaks for your code, you will have what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文