【问题标题】:How do I get an image to save after rotation at runtime?如何在运行时旋转后保存图像?
【发布时间】:2013-02-21 20:10:33
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 StackOverflow!请阅读FAQ关于How to ask
  • 转换由渲染图形(设备上下文等)执行,而不是源图像。如果要保存转换后的图像,则需要将转换后的图像渲染到适当类型的缓冲区,然后打印出来。

标签: c# wpf image printing richtextbox


【解决方案1】:

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

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

【讨论】:

  • 我什至还没有开始编写任何代码。我研究的内容是使用 DrawingVisual、RenderBitmap 和 FileStream。我不确定这是否是正确的方向。感谢您提供的代码,我今天将尝试并让您知道我取得的进展 =)
  • _@narohi,我已经发布了应用程序的代码。即使将图像放入容器中,您的代码仍然可以使用吗?
  • 不确定容器是什么意思,但只要您能够访问 BitmapSource 对象,它就可以工作。
  • 为了使图像文件与 RichTextBox 一起显示,它们必须放置在 UI 容器中(我可能错了),但这是我能够成功显示图像的唯一方法。我会再看看你的代码,看看会发生什么,因为我仍然被这个问题困住了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 2018-03-09
  • 2017-02-03
相关资源
最近更新 更多