[英]rescale image before printing
我正在获取表单的屏幕截图,然后将其发送到打印机。 图像太大,偏离了页面的两侧。 在过去的几个小时里,我一直到处都是无济于事。 有人可以协助吗?
当我打开文件本身时,在打印预览中看起来不错。 如果我再从预览中打印就可以了。 但我想做到这一点而无需用户干预。
public void SetupPrintHandler()
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
printDoc.DefaultPageSettings.Landscape = true;
printDoc.Print();
}
private void OnPrintPage(object sender, PrintPageEventArgs args)
{
using (Image image = Image.FromFile(@"C:/temp2.bmp"))
{
Graphics g = args.Graphics;
g.DrawImage(image, 0, 0);
}
}
private void printscreen()
{
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
sc.CaptureWindowToFile(this.Handle, "C:/temp2.bmp", ImageFormat.Bmp);
SetupPrintHandler();
}
因此,为了进行测试,我将刚才所做的工作而不是屏幕快照保存在panel3中,即2个图片框。 所以我正在使用panel3的大小。
Bitmap bmp = new Bitmap(panel3.ClientSize.Width, panel3.ClientSize.Height);
panel3.DrawToBitmap(bmp, panel3.ClientRectangle);
bmp.Save(subPath + file + ".bmp");
再次,它在打印预览上看起来不错,如果我从打印预览中单击“打印”,则打印效果很好。 但是,如果我直接将其发送到打印机,则不合适。 因此,也许这不是大小问题,而是我在不使用打印预览时必须发送到打印机的设置?
更新因此,我可能已经找到了问题。 当您在打印图片时取消选中“适合边框”时,它非常适合。 但是,当我以可以禁用“适合边框”的方式直接发送到打印机时,似乎没有选择
使用此功能可以调整图像大小。 您需要在打印之前选择缩放比例大小。
例如-将“图片”重新缩放为227x171像素。
image = new Bitmap(image, new Size(227, 171));
如果要调整大小并保留图像的外观,请执行以下操作
public Stream ResizeImage(Stream stream, ImageFormat imageFormat, int width, int height)
{
var originalImage = Image.FromStream(stream);
var sourceWidth = originalImage.Width;
var sourceHeight = originalImage.Height;
const int sourceX = 0;
const int sourceY = 0;
var destX = 0;
var destY = 0;
float nPercent;
var nPercentW = ((float)width / sourceWidth);
var nPercentH = ((float)height / sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = Convert.ToInt16((width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = Convert.ToInt16((height - (sourceHeight * nPercent)) / 2);
}
var destWidth = (int)(sourceWidth * nPercent);
var destHeight = (int)(sourceHeight * nPercent);
// specify different formats based off type ( GIF and PNG need alpha channel - 32aRGB 8bit Red 8bit Green 8bit B 8bit Alpha)
Bitmap newImage;
if (imageFormat.Equals(ImageFormat.Png) || imageFormat.Equals(ImageFormat.Gif))
{
newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
}
else
{
newImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}
newImage.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
var newGraphics = Graphics.FromImage(newImage);
// don't clear the buffer with white if we have transparency
if (!imageFormat.Equals(ImageFormat.Png) && !imageFormat.Equals(ImageFormat.Gif))
{
newGraphics.Clear(Color.White);
}
newGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
newGraphics.DrawImage(originalImage,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
newGraphics.Dispose();
originalImage.Dispose();
var memoryStream = new MemoryStream();
newImage.Save(memoryStream, imageFormat);
memoryStream.Position = 0;
return memoryStream;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.