简体   繁体   English

将图像保存到WPF应用程序中的文件保持宽高比

[英]Save Image to file keeping aspect ratio in a WPF app

Hi I trying to scale a png image with a transparent background. 嗨,我试图用透明背景缩放png图像。 I need it to be 250x250 pixel. 我需要它是250x250像素。

Horizontal and vertical centered and keeping the correct aspect ration. 水平和垂直居中并保持正确的纵横比。 The possibility to set a margin. 设定保证金的可能性。

This is what I got so far. 这是我到目前为止所得到的。

var img = new System.Windows.Controls.Image();
var bi = new BitmapImage(new Uri("C://tmp/original.png", UriKind.RelativeOrAbsolute));
img.Stretch = Stretch.Uniform;
img.Width = 250;
img.Height = 250;
img.Source = bi;

var pngBitmapEncoder = new PngBitmapEncoder();

var stream = new FileStream("C://tmp/test3.png", FileMode.Create);

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(img));
pngBitmapEncoder.Save(stream);
stream.Close();

I know that its not using the Image object yet, and therefor just saves the image without scaling it. 我知道它还没有使用Image对象,因此只保存图像而不缩放它。 But I'm having trouble saving the Image object. 但是我在保存Image对象时遇到了问题。 It gives a compile error of cannot convert from 'System.Windows.Controls.Image' to 'System.Uri' 它给出了一个无法从'System.Windows.Controls.Image'转换为'System.Uri'的编译错误

Hope someone can help me :-) 希望可以有人帮帮我 :-)

EDIT 编辑

Updated the code, to the version with the compile error. 将代码更新为具有编译错误的版本。 just changed 刚改变了

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(bi));

to

pngBitmapEncoder.Frames.Add(BitmapFrame.Create(img));

And here is a list of my using 这是我使用的列表

using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Image = System.Windows.Controls.Image;

What you're doing is similar to zooming in an editor on an image and expecting it to be reflected in the underlying image when you save. 你正在做的是类似于放大图像上的编辑器并期望它在保存时反映在底层图像中。 What you'll need to do is create a TransformedBitmap to modify the image and then add that to the Frames. 您需要做的是创建一个TransformedBitmap来修改图像,然后将其添加到Frames。 eg 例如

        var scale = new ScaleTransform(250 / bi.Width, 250 / bi.Height);
        var tb = new TransformedBitmap(bi, scale);
        pngBitmapEncoder.Frames.Add( BitmapFrame.Create(tb));

Update Regarding the aspect Ratio. 关于宽高比的更新

I need it to be 250x250 pixel 我需要它是250x250像素

If the source image doesn't have a 1:1 ratio for height and width the scaling above meets the "I need it be 250X250" but will create distortion. 如果源图像的高度和宽度比率不是1:1,则上面的缩放符合“我需要它是250X250”,但会产生失真。

To get around this you'll need to either crop the image or scale the image so that just one dimension is 250 pixels. 要解决这个问题,您需要裁剪图像或缩放图像,以便只有一个尺寸为250像素。

To crop the image you can either use the Clip Property or a CroppedBitmap . 要裁剪图像,您可以使用Clip属性CroppedBitmap To scale just one dimension you just use one dimension to determine the scale eg new ScaleTransform(250 / bi.Width, 250 / bi.width); 要仅缩放一个维度,您只需使用一个维度来确定比例,例如new ScaleTransform(250 / bi.Width, 250 / bi.width);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM