如何在运行时旋转后保存图像?

时间:2013-03-06 20:35:34

标签: c# wpf image printing richtextbox

我正在使用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");
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个(在第一行替换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;
}

Another option for conversion...

P.S。如果你发布了一些代码并告诉我们更多关于what you have tried的信息,你会在更短的时间内得到更好的答案。