使用像@I liked the old Stack Overflow中提到的XImage.FromStream
,在您发布的代码中,您应该能够直接使用:
var QRCode_BMP = _generalCode.QR_CodeGenerator(AddReviewPath);
MemoryStream streamQR = new MemoryStream();
QRCode_BMP.Save(streamQR, System.Drawing.Imaging.ImageFormat.Jpeg);
gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(XImage.FromStream(streamQR), 0, 0, 100, 100);
QRCode_BMP.Dispose();
streamQR.Close();
gfx.Dispose();
关于使用流进行 XGraphics.DrawImage
(PDFSharp库)的一般用法,这里是我用来将SVG矢量路径数据打印到PDF的一些代码:
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
path.Data = Geometry.Parse("M12.42,14A1.54,1.54,0,0,0,14,12.87l1-4.24C15.12,7.76,15,7,14,7H10l1.48-3.54A1.17,1.17,0,0,0,10.24,2a1.49,1.49,0,0,0-1.08.46L5,7H1v7ZM9.89,3.14A.48.48,0,0,1,10.24,3a.29.29,0,0,1,.23.09S9,6.61,9,6.61L8.46,8H14c0,.08-1,4.65-1,4.65a.58.58,0,0,1-.58.35H6V7.39ZM2,8H5v5H2Z");
path.Fill = new SolidColorBrush(Colors.Black);
double scale = 2;
path.RenderTransform = new ScaleTransform(scale, scale);
Rect bounds = path.Data.GetRenderBounds(null);
bounds.Width = (bounds.Width + 4) * scale;
bounds.Height = (bounds.Height + 4) * scale;
path.Measure(bounds.Size);
path.Arrange(bounds);
RenderTargetBitmap bitmap = new RenderTargetBitmap(
(int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(path);
PngBitmapEncoder encoderPng = new PngBitmapEncoder();
encoderPng.Frames.Add(BitmapFrame.Create(bitmap));
using (MemoryStream stream = new MemoryStream())
{
encoderPng.Save(stream);
gfx.DrawImage(XImage.FromStream(stream), 10, 100, 14.05, 12.05);
}
gfx.DrawImage(...)
并传递您的xImage。 - I liked the old Stack Overflow