• WPF 图片文件 旋转、缩放、翻转


    不是引用System.Drawing命名空间,采用Bitmap逐一像素复制的方法,而是使用WPF的各种变形(Transform)来实现。

    例子有两部分,Part1是文字处理,Part2是图片处理

    DrawingVisual dv = new DrawingVisual();
    DrawingContext dc = dv.RenderOpen();
    //Part1
    var text = new FormattedText("测试",
    System.Globalization.CultureInfo.CurrentCulture,
    FlowDirection.LeftToRight,
    new Typeface(new FontFamily("黑体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), 20, new SolidColorBrush(Colors.Red));
    dc.PushTransform(new ScaleTransform(-1, 1) { CenterX = text.Width / 2 });//左右翻转 注意翻转中心为文字中心
    dc.DrawText(text, new Point(0, 0));
    dc.Pop();
    dc.DrawText(text, new Point(text.Width, 0));//再写一个不翻转的
    //Part2
    ////对开门 = 一个门 + 一个左右翻转的门
    //BitmapImage bi = new BitmapImage(GetPackUri("简欧式门.jpg"));//图片是外部图片,图片的门把手在右边,并且门是对称的
    //Rect destR = new Rect(bi.PixelWidth, 0, bi.PixelWidth, bi.PixelHeight);
    //dc.PushTransform(new ScaleTransform(-1, 1) { CenterX = bi.PixelWidth + bi.PixelWidth / 2 });//翻转
    //dc.DrawImage(bi, destR);//画入

    //dc.Pop();//恢复

    //Rect destL = new Rect(0, 0, bi.PixelWidth, bi.PixelHeight);
    //dc.DrawImage(bi, destL);
    dc.Close();//不要忘记关
    RenderTargetBitmap rtb = new RenderTargetBitmap((int)Math.Ceiling(text.Width * 2), (int)Math.Ceiling(text.Height), 96.0, 96.0, PixelFormats.Default);//转成图片资源
    rtb.Render(dv);
    imgTest.Source = rtb;//图片控件显示

    //wpf引用外部文件

    private Uri GetPackUri(string url)
    {

    string strUrl = "pack://siteoforigin:,,,";

    if (!url.StartsWith("/"))
    {
    strUrl += "/";
    }
    strUrl += url;
    return new Uri(strUrl, UriKind.RelativeOrAbsolute);
    }


    作者:Meteor
    出处:http://www.cnblogs.com/osmeteor/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]

  • 相关阅读:
    关于MySQL错误 2005
    Eclipse如何导入第三方jar包
    Codeforces Round #377 (Div. 2) D. Exams
    18110 Koishi's travel, Satori's travel
    用Java做的类似皇家守卫战的游戏
    Notepad++如何编译、运行Java
    Codeforces Round #341 (Div. 2)--C. Wet Shark and Flowers
    hdu 2120 Ice_cream's world I
    FZU 1851 组合数
    HUST 1599 Multiple
  • 原文地址:https://www.cnblogs.com/osmeteor/p/2716498.html
Copyright © 2020-2023  润新知