本文介绍了使用RenderTargetBitmap渲染带有FlowDirection = RightToLeft的视觉对象可水平翻转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个WPF应用程序(.Net 4.5),该应用程序可以在阿拉伯语和希伯来语操作系统(即FlowDirection = RightToLeft)上运行,并且尊重所有从右到左的约定.

We have a WPF app (.Net 4.5) that runs on Arabic and Hebrew OSes (i.e. FlowDirection=RightToLeft) and respects all right to left conventions.

我们的"CopyAsBitmap"功能在LeftToRight系统上工作正常,但在RightToLeft系统上,RanderTargetBitmap生成的位图已完全镜像-文本和全部.

Our 'CopyAsBitmap' feature works fine on LeftToRight system, but on RightToLeft systems the bitmap produce by RanderTargetBitmap is completely mirrored - text and all.

我们可以暂时替换RightToLeft和LeftToRight,但这从用户体验的角度来看是不可接受的.

We could temporarily swap out RightToLeft and LeftToRight but this is unacceptable from a user experience perspective.

以下是相关代码的精炼:

Here is a distillation of the relevant code:

Visual myVisual; RenderTargetBitmap renderedBitmap =
new RenderTargetBitmap(
(int)myVisual.ActualWidth,
(int)myVisual.ActualHeight,
96, 96,
PixelFormats.Pbgra32);

renderingBitmap.Render(myVisual); Clipboard.SetImage(renderedBitmap);

renderedBitmap.Render(myVisual); Clipboard.SetImage(renderedBitmap);

将图像粘贴到画图"中,一切都将反转.这看起来像是WPF错误.

Paste the image into Paint and everything is reversed. This looks like a WPF bug.

有人对如何解决此问题有任何建议吗?

Anyone have any suggestions how to fix this?

推荐答案

>>"我们的'CopyAsBitmap'功能在LeftToRight系统上运行良好,但在RightToLeft系统上,RanderTargetBitmap生成的位图已完全镜像-文本和全部.

您想要以下屏幕截图的效果吗?

You want the effect of the following screenshot?

我添加了一个布局控件来调整位置和屏幕截图,您可以参考以下代码.

I added a layout control to adjust the position and the screenshot, you can refer the following code.

XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0">
            <MenuItem Header="Setting">
                <MenuItem Header="Screenshots" Click="miScreenshot_Click"/>
            </MenuItem>
        </Menu>
        <Border Grid.Row="1" >
            <Canvas x:Name="myChart" Background="Gray" FlowDirection="RightToLeft">
                <Button  >Left=0,Top=0</Button>
                <Button Canvas.Left="18" Canvas.Top="18"  >Left=18,Top=18</Button>
                <Button Canvas.Right="18" Canvas.Bottom="18" >Right=18,Bottom=18</Button>
                <Button Canvas.Right="0" Canvas.Bottom="0"  >Right=0,Bottom=0</Button>
                <Button Canvas.Right="0" Canvas.Top="0" >Right=0,Top=0</Button>
                <Button Canvas.Left="0" Canvas.Bottom="0"  >Left=0,Bottom=0</Button>
            </Canvas>
        </Border>
       
    </Grid>

XAML.CS:

     private void miScreenshot_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement visual = myChart as FrameworkElement;

            RenderTargetBitmap bmp = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight
                , 96, 96, PixelFormats.Pbgra32);
            bmp.Render(visual);

            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Image files (*.bmp)|*.bmp";
            if (dlg.ShowDialog().Value)
            {
                using (Stream stream = File.Create(dlg.FileName))
                {
                    encoder.Save(stream);
                }

                if (File.Exists(dlg.FileName))
                {
                    Process.Start(dlg.FileName);
                }
            }
        }


最好的问候,


Best Regards,

吕汉楠


这篇关于使用RenderTargetBitmap渲染带有FlowDirection = RightToLeft的视觉对象可水平翻转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:22