简体   繁体   中英

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. Using the JpegBitmapEncoder you can configure the quality, and with the maximum value you will not see a picture degradation, but anyway it present. So, consider using of PNG format instead.

Here is a short snippet how to load and save a JPEG image:

// 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. 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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