调整图像中的WPF [英] Resize image in WPF

查看:273
本文介绍了调整图像中的WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象,我想重新大小,并需要在我的temp文件夹来保存。



我曾尝试是如下:

 的UIElement UIE = CanvasHost.Child; 
INT宽度= 800;
INT高=(INT)((宽/(双)((FrameworkElement的)UIE).WIDTH)*(INT)((FrameworkElement的)UIE).Height);
RenderTargetBitmap RTB =新RenderTargetBitmap(宽度,高度,96,96,PixelFormats.Pbgra32);
rtb.Render(UIE);

串DIR = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+ @\temp\ (!Directory.Exists(DIR))
如果
Directory.CreateDirectory(DIR);

长尺寸= 0;

字符串文件路径= DIR + DateTime.Now.Ticks.ToString()+(isPng的.png:.JPG);
的BitmapEncoder ENC = NULL;

使用(的FileStream FS = File.Create(文件路径))
{
如果(isPng)
ENC =新PngBitmapEncoder();
,否则
ENC =新JpegBitmapEncoder();

enc.Frames.Add(BitmapFrame.Create(RTB));
enc.Save(FS);

尺寸= fs.Length;
}



但是当我这样创建图像它在临时文件夹中保存图像的一部分。 (如图在上述PIC)





我怎么可以重新大小完整的图像?什么?我错过这里。



编辑:
由ERTI-克里斯Eelmaa提到上面的回答正如前面提到的我已经改变了码如下。和它的作品......

 的UIElement UIE = CanvasHost.Child; 
INT宽度= DataCache.Instance.CurrentProject.MaxPhotoEdgeSize;
INT高=(INT)((宽/(双)((FrameworkElement的)UIE).WIDTH)*(INT)((FrameworkElement的)UIE).Height);

RenderTargetBitmap RTB =新RenderTargetBitmap((INT)((FrameworkElement的)UIE).WIDTH,(INT)((FrameworkElement的)UIE).Height,96,96,PixelFormats.Pbgra32);
rtb.Render(UIE);

ImageSource的IM =(ImageSource的)rtb.Clone();
BitmapFrame BP = CreateResizedImage(IM,宽度,高度,1); //通过ERTI-克里斯Eelmaa
串DIR = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)建议的方法+ @\temp\

如果
Directory.CreateDirectory(DIR)(Directory.Exists(DIR)!);

长尺寸= 0;

字符串文件路径= DIR + DateTime.Now.Ticks.ToString()+(isPng的.png:.JPG);
的BitmapEncoder ENC = NULL;

使用(的FileStream FS = File.Create(文件路径))
{
如果(isPng)
ENC =新PngBitmapEncoder();
,否则
ENC =新JpegBitmapEncoder();


enc.Frames.Add(BitmapFrame.Create(BP));
enc.Save(FS);

尺寸= fs.Length;
}


解决方案

只需使用此方法来获取BitmapFrame,之后你可以将它保存使用PngBitmapEncoder到硬盘。

 私有静态BitmapFrame CreateResizedImage(ImageSource的来源,INT宽度,INT高度,诠释保证金)
{
变种RECT =新的矩形(保证金,保证金,宽 - 保证金* 2,高度 - 保证金* 2);

VAR组=新DrawingGroup();
RenderOptions.SetBitmapScalingMode(组BitmapScalingMode.HighQuality);
group.Children.Add(新ImageDrawing(源,RECT));

变种drawingVisual =新DrawingVisual();
使用(VAR的DrawingContext = drawingVisual.RenderOpen())
drawingContext.DrawDrawing(组);

变种resizedImage =新RenderTargetBitmap(
宽度,高度,调整大小//尺寸
96,96,//默认的DPI值
PixelFormats.Default); //默认像素格式
resizedImage.Render(drawingVisual);

返回BitmapFrame.Create(resizedImage);
}


i have a image and i want to re size it and need to save in my temp folder.

what i have tried is as below :

UIElement uie = CanvasHost.Child;
int width = 800;
int height = (int)((width / (double)((FrameworkElement)uie).Width) * (int)((FrameworkElement)uie).Height);          
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(uie);

string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\temp\";
if (!Directory.Exists(dir))
      Directory.CreateDirectory(dir);

long size = 0;

string filePath = dir + DateTime.Now.Ticks.ToString() + (isPng ? ".png" : ".jpg");
BitmapEncoder enc = null;

using (FileStream fs = File.Create(filePath))
{
    if (isPng)
        enc = new PngBitmapEncoder();
    else
        enc = new JpegBitmapEncoder();

    enc.Frames.Add(BitmapFrame.Create(rtb));
    enc.Save(fs);

    size = fs.Length;
}

but when i create image like this it saves part of the image in temp folder. (as shown in the above pic)

how can i re size full image? what i missed here?

EDIT : As mentioned in the above answer as mentioned by Erti-Chris Eelmaa i have changed the code as below. and it works......

UIElement uie = CanvasHost.Child;
int width = DataCache.Instance.CurrentProject.MaxPhotoEdgeSize;
int height = (int)((width / (double)((FrameworkElement)uie).Width) * (int)((FrameworkElement)uie).Height);

RenderTargetBitmap rtb = new RenderTargetBitmap((int)((FrameworkElement)uie).Width, (int)((FrameworkElement)uie).Height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(uie);

ImageSource im = (ImageSource)rtb.Clone();
BitmapFrame bp = CreateResizedImage(im, width, height, 1); //method suggested by Erti-Chris Eelmaa
string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\temp\";

if (!Directory.Exists(dir))
    Directory.CreateDirectory(dir);

long size = 0;

string filePath = dir + DateTime.Now.Ticks.ToString() + (isPng ? ".png" : ".jpg");
BitmapEncoder enc = null;

using (FileStream fs = File.Create(filePath))
{
    if (isPng)
         enc = new PngBitmapEncoder();
    else
         enc = new JpegBitmapEncoder();


    enc.Frames.Add(BitmapFrame.Create(bp));
    enc.Save(fs);

    size = fs.Length;
}

解决方案

Just use this method to obtain BitmapFrame, after that you can just save it to HDD using PngBitmapEncoder.

private static BitmapFrame CreateResizedImage(ImageSource source, int width, int height, int margin)
{
    var rect = new Rect(margin, margin, width - margin * 2, height - margin * 2);

    var group = new DrawingGroup();
    RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality);
    group.Children.Add(new ImageDrawing(source, rect));

    var drawingVisual = new DrawingVisual();
    using (var drawingContext = drawingVisual.RenderOpen())
        drawingContext.DrawDrawing(group);

    var resizedImage = new RenderTargetBitmap(
        width, height,         // Resized dimensions
        96, 96,                // Default DPI values
        PixelFormats.Default); // Default pixel format
    resizedImage.Render(drawingVisual);

    return BitmapFrame.Create(resizedImage);
}

这篇关于调整图像中的WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆