1.使用bitmap
//fuc:把字符串s存放在一个bitmap中,该bitmap是通过一定的旋转得到的
public static Bitmap TextToBitmap(string s, System.Windows.Media.FontFamily fontType, int fontsize, RotateFlipType offAngle)
{
FormattedText text = new FormattedText(s,
new System.Globalization.CultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(fontType, FontStyles.Normal,
FontWeights.Normal, new FontStretch()),
fontsize, System.Windows.SystemColors.GrayTextBrush);
int bmpW = (int)text.Width+1; //字符串宽度像素
int bmpH = (int)text.Height+1; //字符串高度像素
Bitmap bmpTmp = new Bitmap(bmpW, bmpH); //根据字符串以及字体创建合适大小的bitmap
Graphics grap = Graphics.FromImage(bmpTmp); //创建Graphics
grap.FillRectangle(System.Drawing.Brushes.White, new Rectangle(0, 0, bmpW, bmpH)); //把rtnBmp涂成白色
//将bitmap转换为imagesource供引用
IntPtr ptr = bmpTmp.GetHbitmap();
BitmapSource bmpSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr); //释放资源
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(bmpSource, new Rect(0, 0, bmpW, bmpH));
drawingContext.DrawText(PrintUtils.DrawText(s, fontsize), new System.Windows.Point(0, 0)); //在图片上面写字
drawingContext.Close();
RenderTargetBitmap composeImage = new RenderTargetBitmap(bmpSource.PixelWidth, bmpSource.PixelHeight, bmpSource.DpiX, bmpSource.DpiY, PixelFormats.Default);
composeImage.Render(drawingVisual);
BmpBitmapEncoder bitmapEncoder = new BmpBitmapEncoder(); //bmp编码器
bitmapEncoder.Frames.Add(BitmapFrame.Create(composeImage)); //加载图片
MemoryStream stream = new MemoryStream(); //创建bmp流
bitmapEncoder.Save(stream);
Bitmap rtnBmp = new Bitmap(stream);
rtnBmp.RotateFlip(offAngle);
string name = s.Substring(0,2) + ".bmp";
rtnBmp.Save(name);
return rtnBmp;
}
2.使用drawtext
PrintDialog printDialog = new PrintDialog();
DrawingVisual dv = new DrawingVisual();
var dc = dv.RenderOpen();
dc.DrawImage(imgLookLogo, new Rect(6, 6, 250, 114));
dc.DrawImage(src, new Rect(149, 11, PrintUtils.QR_LOOK_WIDTH, PrintUtils.QR_LOOK_HEIGHT));
//dc.DrawImage(snLogo, new Rect(117, 12, 24, 110));
dc.DrawText((PrintUtils.DrawText("L 000 982", 20)), new System.Windows.Point(15, 8));
string snStr = "L 000 982";
int fontsize = 20;
System.Windows.Media.FontFamily fontType = new System.Windows.Media.FontFamily("微软雅黑");
FormattedText text = new FormattedText(snStr,
new System.Globalization.CultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(fontType, FontStyles.Normal,
FontWeights.Normal, new FontStretch()),
fontsize, System.Windows.SystemColors.GrayTextBrush);
int bmpW = (int)text.Width + 1; //字符串宽度像素
int bmpH = (int)text.Height + 1; //字符串高度像素
dc.PushTransform(new RotateTransform(90, 149, 6));
dc.DrawText((PrintUtils.DrawText("L 000 982", 20)), new System.Windows.Point(149, 6));
dc.Pop();
dc.Close();
639

被折叠的 条评论
为什么被折叠?



