C#如何将文件转换为内存流? [英] C# how to convert files to memory streams?
本文介绍了C#如何将文件转换为内存流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的情况下,我正在研究将PDF转换为BMP,然后将BMP转换为PDF。
我确实包含输入和输出的文件。顺便说一句,我想制作内存流而不是文件,但我想保留a.pdf(输入文件)和a_bmp.pdf(输出文件)。我标记了(#)。我想在这部分(#)中将文件转换为内存流。
这是我的代码。你能告诉我如何将文件转换成内存流吗?
我尝试了什么:
In my case, I am studying to convert PDF to BMP and then convert BMP to PDF.
I did consist of files which input and output. By the way, I would like to make memory streams instead of files but I would like to keep "a.pdf"(input file) and "a_bmp.pdf"(output file). I marked (#). I would like to convert files to memory streams in this part(#).
Here is my code. Could you please advise me how to convert files to memory streams?
What I have tried:
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("a.pdf");
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
string f = String.Format("{0:D2}", pageCount);
using (FileStream imageStream = new FileStream(#"a_" + f + "_out" + ".bmp", FileMode.Create))
{
Resolution resolution = new Resolution(100);
BmpDevice bmpDevice = new BmpDevice(resolution);
bmpDevice.Process(pdfDocument.Pages[pageCount], imageStream);
imageStream.Close();
}
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(#"a_" + f + "_out" + ".bmp"**))
{
Aspose.Imaging.ImageOptions.BmpOptions Settings = new
Aspose.Imaging.ImageOptions.BmpOptions();
Settings.BitsPerPixel = 24;
image.Save(#"a_" + f + "_out.bmp", Settings);
}
}
Pdf pdf1 = new Pdf();
foreach (string f in Directory.GetFiles(#"\\", "*_out.bmp"))
{
FileStream fs = new FileStream(f, FileMode.Open, FileAccess.Read);
byte[] tmpBytes = new byte[fs.Length];
fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));
MemoryStream mystream = new MemoryStream(tmpBytes);
Bitmap b = new Bitmap(mystream);
Aspose.Pdf.Generator.Section sec1 = new Aspose.Pdf.Generator.Section(pdf1);
sec1.PageInfo.Margin.Top = 0;
sec1.PageInfo.Margin.Bottom = 0;
sec1.PageInfo.Margin.Left = 0;
sec1.PageInfo.Margin.Right = 0;
pdf1.Sections.Add(sec1);
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
sec1.Paragraphs.Add(image1);
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
image1.ImageInfo.ImageStream = mystream;
image1.ImageScale = 1;
}
pdf1.Save("a_bmp.pdf");
推荐答案
这样的东西?
Something like this?
IO.FileStream fs = new IO.FileStream(myFile, IO.FileMode.Open, IO.FileAccess.Read);
IO.MemoryStream ms = new IO.MemoryStream();
fs.CopyTo(ms);
这篇关于C#如何将文件转换为内存流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文