0

我正在使用 WPF,并且我有一个应用程序,用户将图像文件加载到 aRichTextBox中,他们可以旋转图像并打印它。我不确定为什么旋转后的图像在屏幕上显示时不会打印。相反,它会打印原件。我是新手,所以任何帮助将不胜感激!

以下是我的应用程序的代码。单击检索文件时的代码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");
        }
    }
4

1 回答 1

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

转换的另一种选择...

PS 如果您发布一些代码并告诉我们更多有关您尝试过的内容,您会在更短的时间内得到更好的答案。

于 2013-03-06T21:39:46.783 回答