gpt4 book ai didi

c# - 如何在多页中打印一张大图像?

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:55 26 4
gpt4 key购买 nike

我想在多页中打印一个高(长)图像。因此,在每一页中,我都会从图像中选取合适的部分并将其绘制在页面中。

问题是我在页面中缩小了图像(它的形状被压缩了),所以我添加了一个比例,它的值为 1500 。我认为,如果我知道以像素为单位的页面高度 (e.Graphics),就可以解决问题。将英寸转换为像素,我必须乘以 (e.Graphics.DpiX = 600) 还是乘以 96。

void printdocument_PrintPage(object sender, PrintPageEventArgs e)
{
if (pageImage == null)
return;
e.Graphics.PageUnit = GraphicsUnit.Pixel;

e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;


float a = (e.MarginBounds.Width / 100) * e.Graphics.DpiX;
float b = ((e.MarginBounds.Height / 100) * e.Graphics.DpiY);
int scale = 1500;
scale = 0; //try to comment this
RectangleF srcRect = new RectangleF(0, startY, pageImage.Width, b - scale);
RectangleF destRect = new RectangleF(0, 0, a, b);
e.Graphics.DrawImage(pageImage, destRect, srcRect, GraphicsUnit.Pixel);
startY = Convert.ToInt32(startY + b - scale);
e.HasMorePages = (startY < pageImage.Height);
}

能否请您让它正常工作。

您可以从 ( here) 下载源代码。

提前致谢。

最佳答案

我试图完成你的任务。干得好。希望对您有所帮助。

此方法将图像打印在多页上(如果图像很小,则打印在一页上)。

private void printImage_Btn_Click(object sender, EventArgs e)
{
list = new List<Image>();
Graphics g = Graphics.FromImage(image_PctrBx.Image);
Brush redBrush = new SolidBrush(Color.Red);
Pen pen = new Pen(redBrush, 3);
decimal xdivider = image_PctrBx.Image.Width / 595m;
int xdiv = Convert.ToInt32(Math.Ceiling(xdivider));
decimal ydivider = image_PctrBx.Image.Height / 841m;
int ydiv = Convert.ToInt32(Math.Ceiling(ydivider));
/*int xdiv = image_PctrBx.Image.Width / 595; //This is the xsize in pt (A4)
int ydiv = image_PctrBx.Image.Height / 841; //This is the ysize in pt (A4)
// @ 72 dots-per-inch - taken from Adobe Illustrator

if (xdiv >= 1 && ydiv >= 1)
{*/
for (int i = 0; i < xdiv; i++)
{
for (int y = 0; y < ydiv; y++)
{
Rectangle r;
try
{
r = new Rectangle(i * Convert.ToInt32(image_PctrBx.Image.Width / xdiv),
y * Convert.ToInt32(image_PctrBx.Image.Height / ydiv),
image_PctrBx.Image.Width / xdiv,
image_PctrBx.Image.Height / ydiv);
}
catch (Exception)
{
r = new Rectangle(i * Convert.ToInt32(image_PctrBx.Image.Width / xdiv),
y * Convert.ToInt32(image_PctrBx.Image.Height),
image_PctrBx.Image.Width / xdiv,
image_PctrBx.Image.Height);
}


g.DrawRectangle(pen, r);
list.Add(cropImage(image_PctrBx.Image, r));
}
}

g.Dispose();
image_PctrBx.Invalidate();
image_PctrBx.Image = list[0];

PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocument_PrintPage;
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
previewDialog.Document = printDocument;
pageIndex = 0;
previewDialog.ShowDialog();
// don't forget to detach the event handler when you are done
printDocument.PrintPage -= PrintDocument_PrintPage;
}

此方法以所需尺寸(A4 尺寸)打印列表中的每张图片:

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Draw the image for the current page index
e.Graphics.DrawImageUnscaled(list[pageIndex],
e.PageBounds.X,
e.PageBounds.Y);
// increment page index
pageIndex++;
// indicate whether there are more pages or not
e.HasMorePages = (pageIndex < list.Count);
}

此方法裁剪图像并返回图像的每个部分:

    private static Image cropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, System.Drawing.Imaging.PixelFormat.DontCare);
return (Image)(bmpCrop);
}

图像从 PictureBox 加载,因此请确保图像已加载。 (还没有异常处理)。

    internal string tempPath { get; set; }
private int pageIndex = 0;
internal List<Image> list { get; set; }

这些变量被定义为全局变量。

您可以在此处下载示例项目:

http://www.abouchleih.de/projects/PrintImage_multiplePages.zip // OLD Version http://www.abouchleih.de/projects/PrintImage_multiplePages_v2.zip // NEW

关于c# - 如何在多页中打印一张大图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14075844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com