将 WPF InkCanvas 保存为 JPG - 图像被裁剪
我有一个 WPF InkCanvas 控件,用于捕获应用程序中的签名。 该控件看起来像这样 - 700x300
但是,当我将其另存为 JPG 时,生成的图像看起来像这样,也是 700x300
我用来保存的代码
sigPath = System.IO.Path.GetTempFileName();
MemoryStream ms = new MemoryStream();
FileStream fs = new FileStream(sigPath, FileMode.Create);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkSig.Width, (int)inkSig.Height, 96d, 96d, PixelFormats.Default);
rtb.Render(inkSig);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fs);
fs.Close();
这是我正在使用的 XAML:
<Window x:Class="Consent.Client.SigPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent" Topmost="True" AllowsTransparency="True"
Title="SigPanel" Left="0" Top="0" Height="1024" Width="768" WindowStyle ="None" ShowInTaskbar="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" >
<Border BorderThickness="1" BorderBrush="Black" Background='#FFFFFFFF' x:Name='DocumentRoot' Width='750' Height='400' CornerRadius='10'>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Name="txtLabel" FontSize="24" HorizontalAlignment="Center" >Label</TextBlock>
<InkCanvas Opacity="1" Background="Beige" Name="inkSig" Width="700" Height="300" />
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button FontSize="24" Margin="10" Width="150" Name="btnSave" Click="btnSave_Click">Save</Button>
<Button FontSize="24" Margin="10" Width="150" Name="btnCancel" Click="btnCancel_Click">Cancel</Button>
<Button FontSize="24" Margin="10" Width="150" Name="btnClear" Click="btnClear_Click">Clear</Button>
</StackPanel>
</StackPanel>
</Border>
在过去,这非常有效。 我无法弄清楚是什么变化导致图像在保存时发生变化。
I have a WPF InkCanvas control I'm using to capture a signature in my application. The control looks like this - it's 700x300
However, when I save it as a JPG, the resulting image looks like this, also 700x300
The code I'm using to save
sigPath = System.IO.Path.GetTempFileName();
MemoryStream ms = new MemoryStream();
FileStream fs = new FileStream(sigPath, FileMode.Create);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkSig.Width, (int)inkSig.Height, 96d, 96d, PixelFormats.Default);
rtb.Render(inkSig);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fs);
fs.Close();
This is the XAML I'm using:
<Window x:Class="Consent.Client.SigPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent" Topmost="True" AllowsTransparency="True"
Title="SigPanel" Left="0" Top="0" Height="1024" Width="768" WindowStyle ="None" ShowInTaskbar="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" >
<Border BorderThickness="1" BorderBrush="Black" Background='#FFFFFFFF' x:Name='DocumentRoot' Width='750' Height='400' CornerRadius='10'>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Name="txtLabel" FontSize="24" HorizontalAlignment="Center" >Label</TextBlock>
<InkCanvas Opacity="1" Background="Beige" Name="inkSig" Width="700" Height="300" />
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button FontSize="24" Margin="10" Width="150" Name="btnSave" Click="btnSave_Click">Save</Button>
<Button FontSize="24" Margin="10" Width="150" Name="btnCancel" Click="btnCancel_Click">Cancel</Button>
<Button FontSize="24" Margin="10" Width="150" Name="btnClear" Click="btnClear_Click">Clear</Button>
</StackPanel>
</StackPanel>
</Border>
In the past this worked perfectly. I can't figure out what changed that is causing the image to shift when it is saved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我也遇到了同样的问题,我这样做了..它在这里工作..
I had same problem i did this way.. It worked here..
和
surface.Margin = new Thickness(55,40,96,5);
http://img519.imageshack.us/img519/7499/mynewimage.png
and
surface.Margin = new Thickness(55,40,96,5);
http://img519.imageshack.us/img519/7499/mynewimage.png
我的班级保存图像
My class save image
啊哈! 问题在于位于 InkCanvas 正上方的 TextBlock txtLabel。 当你删除它时,黑线就会消失。
至于为什么会发生这种情况,我还不完全确定。
Aha! The problem is the TextBlock txtLabel that is directly above the InkCanvas. When you remove that the black line disappears.
As for why that is happening, I'm not entirely sure yet.
杰森,我解决了这个问题。
对不起我的英语。 我是俄罗斯人。
您需要在
0,0,0,0
设置属性inkCanvas.Margin
with:
在您的仓位保存设置保证金后。
例子:
http://img189.imageshack.us/img189/7499/mynewimage.png
Jason, I solved this problem.
Sorry my English. I am Russian.
You need set property
inkCanvas.Margin
at0,0,0,0
with:
after saving set margin at your position.
example:
http://img189.imageshack.us/img189/7499/mynewimage.png
我一直在网上寻找这个问题的答案,并尝试了大多数意见,但没有任何乐趣。 然后我尝试了这个并且成功了!
I've been looking all over the net for an answer to this problem and tried most opinions without any joy. Then I tried this and it worked!