我在内存流中创建了一个二维码图像,将其保存到我的硬盘驱动器中,然后我可以将其插入到 Word 文档中。是否可以将其保存在文档中而不保存在硬盘上?二维码创建代码
var bitmapData = bitmap.LockBits(new System.DrawingCore.Rectangle(0, 0, pixelData.Width, pixelData.Height),
System.DrawingCore.Imaging.ImageLockMode.WriteOnly, System.DrawingCore.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// save to stream as PNG
bitmap.Save(@"c:\ggg.png", System.DrawingCore.Imaging.ImageFormat.Png);
这是我加载文档的方式:
using (FileStream stream = new FileStream(@"c:\ggg.png", FileMode.Open))
//using (FileStream stream = new FileStream(memoryStreamBitmap, FileMode.Open))
{
image.FeedData(stream);
}
// image.FeedData(memoryStreamBitmap);
AddImageToBody(wordDocument, mainPart.GetIdOfPart(image));
是的,确实有必要保存内存流并将流传递给将保存文档的方法并指定 并保存文档
msBitmap.Position = 0;var msBitmap = new MemoryStream();bitmap.Save(msBitmap, System.DrawingCore.Imaging.ImageFormat.Png); return msBitmap;
msBitmap.Position = 0; image.FeedData(msBitmap);