文档半岛外围网上直营>>E-iceblue中文文档>>将 RTF 转换为图像并重置图像分辨率
将 RTF 转换为图像并重置图像分辨率
Spire.Doc具有在 C# 和 VB.NET 中操作 RTF 文件格式的强大能力。通过使用 Spire.Doc,开发人员可以将 RTF 转换为 PDF、HTML和 .doc、.docx 格式的 word 文档。本文将向您展示如何将 RTF 转换为图像,然后重置图像分辨率。
下载并安装 Spire.Doc for .NET,然后通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll”。下面详细介绍如何在 C# 中将 RTF 转换为 PNG 和重置图像分辨率。
第 1 步:创建一个新文档并从文件加载。
Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
第 2 步:将 RTF 保存到图像。
Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
第 3 步:遍历图片列表中的元素,保存为.Png格式。
for (int i = 0; i < images.Length; i++)
{
Metafile mf = images[i] as Metafile;
Image newimage = ResetResolution(mf, 200);
string outputfile = String.Format("image-{0}.png", i);
newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png);
}
第 4 步:设置图像分辨率调用方法:ResetResolution。
public static Image ResetResolution(Metafile mf, float resolution)
{
int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
int height = (int)(mf.Height * resolution / mf.VerticalResolution);
Bitmap bmp = new Bitmap(width, height);
bmp.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(mf, Point.Empty);
}
return bmp;
}
重置图片分辨率前图片的有效截图:
重置图像分辨率后的图像:
完整代码:
using Spire.Doc;
using System.Drawing;
using System.Drawing.Imaging;
namespace RTFtoImage
{
class Program
{
static void Main(string[] args)
{
//Create a new document and load from file.
Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
// save the RTF to image
Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
for (int i = 0; i < images.Length; i++)
{
Metafile mf = images[i] as Metafile;
Image newimage = ResetResolution(mf, 200);
string outputfile = String.Format("image-{0}.png", i);
newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png);
}
}
//set the image resolution by the ResetResolution() method
public static Image ResetResolution(Metafile mf, float resolution)
{
int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
int height = (int)(mf.Height * resolution / mf.VerticalResolution);
Bitmap bmp = new Bitmap(width, height);
bmp.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(mf, Point.Empty);
}
return bmp;
}
}
}
欢迎下载|体验更多E-iceblue产品
如需获取更多产品相关信息请咨询


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线