文檔半島外圍網(wǎng)上直營>>E-iceblue中文文檔>>將 RTF 轉(zhuǎn)換為圖像并重置圖像分辨率
                將 RTF 轉(zhuǎn)換為圖像并重置圖像分辨率
Spire.Doc具有在 C# 和 VB.NET 中操作 RTF 文件格式的強大能力。通過使用 Spire.Doc,開發(fā)人員可以將 RTF 轉(zhuǎn)換為 PDF、HTML和 .doc、.docx 格式的 word 文檔。本文將向您展示如何將 RTF 轉(zhuǎn)換為圖像,然后重置圖像分辨率。
下載并安裝 Spire.Doc for .NET,然后通過以下路徑在下載的 Bin 文件夾中添加 Spire.Doc.dll 作為參考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll”。下面詳細介紹如何在 C# 中將 RTF 轉(zhuǎn)換為 PNG 和重置圖像分辨率。
第 1 步:創(chuàng)建一個新文檔并從文件加載。
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 步:設(shè)置圖像分辨率調(diào)用方法: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產(chǎn)品
如需獲取更多產(chǎn)品相關(guān)信息請咨詢


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線