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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM