为什么生成的“PDF文件”只有一半??? 怪哉。。。 可参考的Demo地址:http://www.cnblogs.com/yjmyzz/archive/2010/03/09/1681389.html ,用的“SilverPDF”。。。
1、Silverlight展示结果。
2、PDF导出结果
导出方法:
1、XAML如下:
<Grid x:Name="gchart">
<vc:Chart Theme="Theme1" View3D="False" UniqueColors="False" LightingEnabled="False" Background="#FFF5F5DF" Name="SLChart" IndicatorEnabled="True">
<vc:Chart.Titles>
<vc:Title Text="为什么只有一个半" FontColor="#000000" FontSize="20" />
<vc:Title Text="" FontColor="#000000" FontSize="15" />
</vc:Chart.Titles>
<vc:Chart.AxesY>
<vc:Axis LineColor="#000000"
LineThickness="1"
Title="为什么"
Enabled="True"
StartFromZero="True"
AxisType="Primary"
LineStyle="Solid">
<vc:Axis.AxisLabels>
<vc:AxisLabels FontSize="13"/>
</vc:Axis.AxisLabels>
</vc:Axis>
</vc:Chart.AxesY>
<vc:Chart.AxesX>
<vc:Axis LineColor="#000000"
Interval="1"
LineThickness="1"
Enabled="True"
AxisType="Primary"
LineStyle="Solid">
<vc:Axis.AxisLabels>
<vc:AxisLabels FontSize="13"/>
</vc:Axis.AxisLabels>
</vc:Axis>
</vc:Chart.AxesX>
<vc:Chart.Series>
</vc:Chart.Series>
</vc:Chart>
</Grid>
2、后台代码如下
public ElectricityMonitoringFunc()
{
InitializeComponent();
//加载中文字库
var fs = Application.GetResourceStream(
new Uri("MyCharts;component/Fonts/STSONG.TTF", UriKind.RelativeOrAbsolute)).Stream;
FontPool.Register(fs, "Huxtable");
问题一:为什么初始化页面,要引入中文字库??? "TTF"文件就是中文字库了??? “FontPool.Register”方法是干什么使的???
}
void CreateImgPdf()
{
//WriteableBitmap wb = new WriteableBitmap(SLChart, null);
WriteableBitmap wb = new WriteableBitmap(gchart, null);
byte[] b = Convert.FromBase64String(GetBase64Image(wb));
MemoryStream ms = new MemoryStream(b);
XImage img = XImage.FromStream(ms);
SaveFileDialog d = new SaveFileDialog();
d.Filter = "PDF file format|*.pdf";
// Save the document...
if (d.ShowDialog() == true)
{
document = new PdfDocument();
document.Info.Title = "为什么只显示一半";
document.Info.Author = "Why";
document.Info.Subject = "利用WriteableBitmap生成PDF图片";
document.Info.Keywords = "Silverlight,PDF";
XGraphics gfx = XGraphics.FromPdfPage(document.AddPage());
gfx.DrawImage(img, new Point() { X = 0, Y = 0 });
document.Save(d.OpenFile());
}
ms.Close();
ms.Dispose();
}
/// <summary>
/// 老外写的方法:将WriteableBitmap转化为base64位字符串
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private string GetBase64Image(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth ;
int height = bitmap.PixelHeight ;
int bands = 3;
byte[][,] raster = new byte[bands][,];
for (int i = 0; i < bands; i++)
{
raster[i] = new byte[width, height];
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
int pixel = bitmap.Pixels[width * row + column];
raster[0][column, row] = (byte)(pixel >> 16);
raster[1][column, row] = (byte)(pixel >> 8);
raster[2][column, row] = (byte)pixel;
}
}
ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
MemoryStream stream = new MemoryStream();
JpegEncoder encoder = new JpegEncoder(img, 100, stream);
encoder.Encode();
stream.Seek(0, SeekOrigin.Begin);
byte[] binaryData = new Byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
string base64String =
System.Convert.ToBase64String(binaryData,
0,
binaryData.Length);
return base64String;
}
问题二:为什么调用“ CreateImgPdf()”方法后,如上图所示,导出的图片只有一半??? Why???