简体   繁体   English

C#WPF图像加载和保存

[英]C# WPF Image loading and saving

I have been trying to load my image in bitmap image class. 我一直试图在位图图像类中加载我的图像。 But whenever I am trying to save an image (using a third party library called Image Processor) the quality drops. 但是,每当我尝试保存图像(使用名为“图像处理器”的第三方库)时,质量都会下降。 So my question is that: 所以我的问题是:

Is there any way i can save image in its full quality? 有什么方法可以保存完整质量的图像?

src.BeginInit();

src.UriSource = new Uri("picture.jpg", UriKind.Relative);

src.CacheOption = BitmapCacheOption.OnLoad;

src.EndInit(); 

You can achieve image saving without any thirdparty libraries. 您无需任何第三方库就可以实现图像保存。 But you should know, that saving JPEG always drops quality a bit - its a nature of that format, not an issue with library. 但是您应该知道,保存JPEG总是会降低质量-这是该格式的本质,而不是库的问题。 Using the JpegBitmapEncoder you can configure the quality, and with the maximum value you will not see a picture degradation, but anyway it present. 使用JpegBitmapEncoder可以配置质量,并使用最大值不会看到图像质量下降,但无论如何都会出现。 So, consider using of PNG format instead. 因此,请考虑改用PNG格式。

Here is a short snippet how to load and save a JPEG image: 以下是如何加载和保存JPEG图像的简短代码段:

// Load from file or any other source.
var uri = new Uri(@"D:\InputImage.jpg");
var bitmap = new BitmapImage(uri);

// Save to file.
var encoder = new JpegBitmapEncoder(); // Or any other, e.g. PngBitmapEncoder for PNG.
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.QualityLevel = 100; // Set quality level 1-100.

using (var stream = new FileStream(@"D:\OutputImage.jpg", FileMode.Create))
{
    encoder.Save(stream);
}

WebImage works very well and I use often this class in my application. WebImage效果很好,我在应用程序中经常使用此类。 It has a lot of nice built-in features, for example, saving proportins when resizing an image: 它具有许多不错的内置功能,例如,在调整图像大小时保存比例信息:

 WebImage wi = new WebImage(imageName);
 wi.Resize(314, 235, true);
 wi.Save(imageName, "png");

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

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