Save WriteableBitmap to file using WPF - c#

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.

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

Related

Jpeg2000 with transparency

I'm trying to use Magick.Net to convert a transparent Png to Jpeg2000 format and keep its transparency. The code is rather simple:
using(FileStream source = new FileStream(#"D:\test.png", FileMode.Open))
using(FileStream file = new FileStream(#"D:\test.jp2", FileMode.Create))
using (MagickImage image = new MagickImage(source))
{
image.Format = MagickFormat.Jp2;
image.Write(file);
}
The source image: and result:
As you can see, the image lost its transparency, while both images are 24BPP. What am I missing here?

Is there any way to write EXIF data in PNG using c#

I know that EXIF MetaData is not supported in PNG (as per W3C), but i have a tool which can inject EXIF MetaData in PNG in zTXt block, i have searched everywhere but did'nt find anyway how to write EXIF data in PNG using c# using sqtquery or any other way.
As long as you can write your data as text, this is a straightforward solution:
public void WriteTextInPngFile(string filename, string description, string otherText)
{
Stream pngStream = new System.IO.FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
PngBitmapDecoder pngDecoder = new PngBitmapDecoder(pngStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapFrame pngFrame = pngDecoder.Frames[0];
InPlaceBitmapMetadataWriter pngInplace = pngFrame.CreateInPlaceBitmapMetadataWriter();
if (pngInplace.TrySave() == true)
{
pngInplace.SetQuery("/Text/Description", description);
pngInplace.SetQuery("/Text/YourField", otherText);
}
pngStream.Close();
}
You have to set the following references:
PresentationCore
System.Xaml
WindowsBase
This example is based on the help of BitmapFrame Class

BitMapEncoder in MonoTouch

I'm currently in the process of converting some standard C# files so they work in a MonoTouch iPhone application, however I have hit a wall with this method:
private void WriteTestFile(CGBitmapContext bitmapImage, string fileName)
{
try
{
using (FileStream stream5 = new FileStream(fileName, FileMode.Create))
{
BitmapEncoder encoder5 = new BmpBitmapEncoder();
encoder5.Frames.Add(BitmapFrame.Create(bitmapImage));
encoder5.Save(stream5);
stream5.Close();
}
}
catch (UnauthorizedAccessException)
{
// Do nothing
}
}
I can't seem to find any equivalent to the BitmapEncoder class and I have no idea how to approach converting this part of the code, can anyone suggest a route to take?
Note: The original code used a WritableBitMap in pace of the CGBitmapContext.
I know nothing about MonoTouch nor iPhone.
However it looks like the function is just trying to save the bitmap image as BMP file. There must be an function in CGBitmapContext to Save to a file or some other class than can save a bitmap.
EDIT
After further search, it looks like you should convert CGBitmapContext to a UIImage and then save it; seet How do I save an UIImage as BMP?

ArgumentException when loading image from byte array using JpegBitmapDecoder

I'm having some trouble reading JPEG files in my class. I need to load metadata and bitmap from a JPEG file. So far, I have this:
public void Load()
{
using (Stream imageStream = File.Open(this.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource source = decoder.Frames[0];
// load metadata
this.metadata = source.Metadata as BitmapMetadata;
// prepare buffer
int octetsPerPixel = source.Format.BitsPerPixel / 8;
byte[] pixelBuffer = new byte[source.PixelWidth * source.PixelHeight * octetsPerPixel];
source.CopyPixels(pixelBuffer, source.PixelWidth * octetsPerPixel, 0);
Stream pixelStream = new MemoryStream(pixelBuffer);
// load bitmap
this.bitmap = new Bitmap(pixelStream); // throws ArgumentException
}
this.status = PhotoStatus.Loaded;
}
But the Bitmap constructor throws an ArgumentException when trying to create a Bitmap instance from a stream.
The documentation says:
System.ArgumentException
stream does not contain image data or is null.
-or-
stream contains a PNG image file with a single dimension greater than 65,535 pixels.
I'm not sure, what I did wrong. Can you please help me?
You're using the Bitmap constructor which is usually used to load an image file in a known format - JPEG, PNG etc. Instead, you've just got a bunch of bytes, and you're not telling it anything about the format you want to use them in.
It's not clear why you want to use BitmapDecoder and BitmapSource at all - why aren't you just using:
Stream imageStream = File.Open(this.FilePath, FileMode.Open,
FileAccess.Read, FileShare.Read));
this.bitmap = new Bitmap(imageStream);
Note that you mustn't use a using statement here - the Bitmap "owns" the stream after you've called the constructor.
Aside from all of this, you seem to be trying to mix WPF and WinForms ideas of images, which I suspect is a generally bad idea :(

What is to FromFile and ToFile equivalent for BitmapImage?

The System.Drawing.Image has the easy to use methods for FromFile and ToFile. What is the equivalent for the Silverlight BitmapImage? I am trying to load and save a jpeg image as part of a unit test. The bytes must match exactly for it to pass. Here is my current guess:
//I am not sure this is right
private BitmapImage GetImage(string fileName)
{
BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
using (Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bitmapImage.BeginInit();
bitmapImage.StreamSource = imageStreamSource;
bitmapImage.EndInit();
}
return bitmapImage;
}
private void SaveImage(BitmapImage bitmapImage, string file)
{
//How to do this?
}
An in-browser Silverlight application cannot open a file using a filename. You would need it run out-of-browser with elevated trust to do that.
Silverlight has no built-in image encoding so you can't take the contents of a bitmap (BTW you would need to be using WriteableBitmap to be able to access the raw image).
You find something you need in Image Tools.
From MSDN link, use the ctor overload which takes in a URI or
BitmapImage myBitmapImage = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(#"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;
To save the image to disk, you'd need the specific encoder e.g. JpegBitmapEncoder

Categories