文字旋转的几种方法

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值