图像保存到文件保持纵横比在一个WPF应用程序应用程序、图像、文件、WPF

2023-09-05 01:58:17 作者:滚开我要变身了

您好我想扩展PNG图像具有透明背景。我需要它是250X250像素。

水平和垂直居中,并保持正确的纵横比。的可能性来设置的余量。

这是我得到了这么远。

  VAR IMG =新System.Windows.Controls.Image();
VAR BI =新的BitmapImage(新的URI(C://tmp/original.png,UriKind.RelativeOrAbsolute));
img.Stretch = Stretch.Uniform;
img.Width = 250;
img.Height = 250;
img.Source =双向;

VAR pngBitmapEn codeR =新PngBitmapEn codeR();

VAR流=新的FileStream(C://tmp/test3.png,FileMode.Create);

pngBitmapEn coder.Frames.Add(BitmapFrame.Create(IMG));
pngBitmapEn coder.Save(流);
stream.Close();
 

我知道,它不使用Image对象着呢,为此只是保存图像,而不进行缩放。但我有保存图像对象的麻烦。它给出了无法从System.Windows.Controls.Image'转换为'的System.Uri

编译错误

希望有人能帮助我: - )

修改

更新了code,与编译错误的版本。只是改变了

  pngBitmapEn coder.Frames.Add(BitmapFrame.Create(BI));
 
我改了一个应用程序的打开方式为 Windows图片查看器

  pngBitmapEn coder.Frames.Add(BitmapFrame.Create(IMG));
 

这里是我使用的列表

 使用系统;
使用System.Drawing中;
使用System.IO;
使用System.Windows;
使用System.Windows.Controls.Primitives;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用图像= System.Windows.Controls.Image;
 

解决方案

你在做什么类似于放大的图像编辑器,并期待它反映底层图像中,当您保存。你需要什么做的就是创建一个 TransformedBitmap 来修改图像,然后添加到帧。 例如,

  VAR规模=新ScaleTransform(250 / bi.Width,250 / bi.Height);
        VAR TB =新TransformedBitmap(BI,规模);
        pngBitmapEn coder.Frames.Add(BitmapFrame.Create(TB));
 

更新关于纵横比。

  

我需要它是250X250像素

如果源图像不具有1:1的比例为高度和宽度以上缩放符合我需要它是250X250,但会产生失真。

要解决这个问题,你要么需要裁剪图像或缩放图像,只有一个尺寸为250像素。

要裁剪图像您可以使用夹物业或 CroppedBitmap 。为了扩展只是一个方面,你只需要使用一个维度来确定规模,例如新ScaleTransform(250 / bi.Width,250 / bi.width);

Hi I trying to scale a png image with a transparent background. I need it to be 250x250 pixel.

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. But I'm having trouble saving the Image object. It gives a compile error of cannot convert from 'System.Windows.Controls.Image' to '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. e.g.

        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

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.

To get around this you'll need to either crop the image or scale the image so that just one dimension is 250 pixels.

To crop the image you can either use the Clip Property or a CroppedBitmap. To scale just one dimension you just use one dimension to determine the scale e.g. new ScaleTransform(250 / bi.Width, 250 / bi.width);