notreadytogiveup123
notreadytogiveup123

Reputation: 21

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(); 

Upvotes: 1

Views: 3043

Answers (2)

kyrylomyr
kyrylomyr

Reputation: 12642

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);
}

Upvotes: 2

StepUp
StepUp

Reputation: 38199

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

Upvotes: 0

Related Questions