Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save WriteableBitmap to file using WPF

I have:

WriteableBitmap bmp;

I basicly want to save it into a file on the disk like the following:

C:\bmp.png

I read some forums which mentions to read:

bmp.Pixels

and save those pixels into a Bitmap then use Bitmap.SaveImage() function. However, I can't access any Pixels. Apperantly my WriteableBitmap does not have any property named Pixels.

I use .NET Framework 4.0.

like image 699
Sait Avatar asked Jun 26 '12 17:06

Sait


1 Answers

Use your WriteableBitmap's clone and use this function as below:

CreateThumbnail(filename, _frontBitmap.Clone());

...

void CreateThumbnail(string filename, BitmapSource image5)
{
    if (filename != string.Empty)
    {
         using (FileStream stream5 = new FileStream(filename, FileMode.Create))
         {
             PngBitmapEncoder encoder5 = new PngBitmapEncoder();
             encoder5.Frames.Add(BitmapFrame.Create(image5));
             encoder5.Save(stream5);
         }
    }
 }
like image 142
Indy9000 Avatar answered Oct 04 '22 15:10

Indy9000