簡體   English   中英

在運行時旋轉后如何保存圖像?

[英]How do I get an image to save after rotation at runtime?

我正在使用WPF,並且我有一個應用程序,用戶可以將圖像文件加載到RichTextBox並且他們可以旋轉圖像並進行打印。 我不確定旋轉后的圖像為什么不打印,因為它顯示在屏幕上。 而是打印原始文件。 我對此並不陌生,因此不勝感激!

以下是我的應用程序的代碼。 單擊檢索文件Button時的代碼:

private void retrieve_button_Click(object sender, RoutedEventArgs e)
{
  //Retrieve the file or image you are looking for
  OpenFileDialog of = new OpenFileDialog();

  of.Filter = "Formats|*.jpg;*.png;*.bmp;*.gif;*.ico;*.txt|JPG Image|*.jpg|BMP image|*.bmp|PNG image|*.png|GIF Image|*.gif|Icon|*.ico|Text File|*.txt";

        var dialogResult = of.ShowDialog();

        if (dialogResult == System.Windows.Forms.DialogResult.OK)
        {               
                try
                {

                    System.Windows.Controls.RichTextBox myRTB = new System.Windows.Controls.RichTextBox();                                     
                {
                    Run myRun = new Run();

                    System.Windows.Controls.Image MyImage = new System.Windows.Controls.Image();
                    MyImage.Source = new BitmapImage(new Uri(of.FileName, UriKind.RelativeOrAbsolute));


                    InlineUIContainer MyUI = new InlineUIContainer();
                    MyUI.Child = MyImage;


                    rotateright_button.IsEnabled = true;
                    print_button.IsEnabled = true;

                    Paragraph paragraph = new Paragraph();
                    paragraph.Inlines.Add(myRun);
                    paragraph.Inlines.Add(MyUI);

                    FlowDocument document = new FlowDocument(paragraph);
                    richTextBox.Document = document;                       
                }
            }

            catch (ArgumentException)
            {
                System.Windows.Forms.MessageBox.Show("Invalid File");
            }

        }
    }

單擊右旋轉按鈕時,將執行以下代碼:

    RotateTransform cwRotateTransform;
    private void rotateright_button_Click(object sender, RoutedEventArgs e)
    {
        richTextBox.LayoutTransform = cwRotateTransform;

        if (cwRotateTransform == null)
        {
            cwRotateTransform = new RotateTransform();
        }

        if (cwRotateTransform.Angle == 360)
        {
            cwRotateTransform.Angle = 0;
        }
        else
        {
            cwRotateTransform.Angle += 90;
        }
    }

加載並旋轉Image后,用戶可以使用以下代碼進行打印:

    private void InvokePrint(object sender, RoutedEventArgs e)
    {
     System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
        if ((bool)printDialog.ShowDialog().GetValueOrDefault())
        {
            FlowDocument flowDocument = new FlowDocument();

            flowDocument = richTextBox.Document;
            flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
            flowDocument.PagePadding = new Thickness(65);
            IDocumentPaginatorSource iDocPag = flowDocument;

            printDialog.PrintDocument(iDocPag.DocumentPaginator, "Print Document");
        }
    }

嘗試以下操作(在第一行中替換yourImageControl,指定所需的RotateFlipType,並確保引用System.Drawing dll):

System.Drawing.Bitmap bitmap = BitmapSourceToBitmap((BitmapSource)YourImageControl.Source);
bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);

public static System.Drawing.Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource)
{
    System.Drawing.Bitmap bitmap;
    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new System.Drawing.Bitmap(outStream);
    }
    return bitmap;
}

轉換的另一種選擇...

PS:如果您發布一些代碼並告訴我們更多有關您嘗試過的內容的信息,那么您將在更短的時間內得到更好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM