Showing posts with label BitMapImage. Show all posts
Showing posts with label BitMapImage. Show all posts

Tuesday, May 13, 2014

Windows Phone 8 photo/image resizing


Problem:

I am trying to wrap up my first phone application which submits text and photos to an email server.
I wanted to control file size since the user can send anything on their phone.  I had been using the BitmapImage captured directly from the e.ChosenPhoto Task.  There are a number of great tutorials on basic photo capturing.  What they overlook is the the whole resizing issue.   The object for that is WriteableBitmap but you can't just cast the BitmapImage to WriteableBitmap.

The BitmapImage is pretty much immutable once you create it.  That is there is no implicit cast to an object like WriteableBitMap.  Most solutions involve reading the content of the BitmapImage as a stream or array into an image and then casting to to WriteableBitmap.


Solution 1:

Mike Ormond's post was very helpful.  He used a memory stream and saved the resulting byte array to the MediaLibrary.

void task_Completed(object sender, PhotoResult e)
{
  if (e.Error == null)
  {
    BitmapImage bi = new BitmapImage();
    bi.SetSource(e.ChosenPhoto);
    Image image1 = new Image()
    {
      Width = 800,
      Height = 600,
      Visibility = System.Windows.Visibility.Collapsed,
      Source = bi
    };
    ScaleTransform st = new ScaleTransform()
    {
      ScaleX = 0.1,
      ScaleY = 0.1
    };
    WriteableBitmap wb = new WriteableBitmap(image1, st);
    ThreadPool.QueueUserWorkItem(callback =>
    {
      MemoryStream ms = new MemoryStream();
      wb.SaveJpeg(ms, 80, 60, 0, 80);
      using (MediaLibrary lib = new MediaLibrary())
        lib.SavePicture("Test", ms.ToArray());
    });
  }
}

Solution 2:
Take the resulting source photo directly to a WriteableBitmap.


WritableBitmap bitmap = new WritableBitmap(); //Use appropriate constructor
bitmap.SetSource(e.ChosenPhoto);
or

ScaleTransform st = new ScaleTransform()
{
ScaleX = 0.1,
ScaleY = 0.1
};
WriteableBitmap wb = new WriteableBitmap(e.ChosenPhoto, st);

Source:
http://msdn.microsoft.com/en-us/library/dd638675(v=vs.95).aspx
http://stackoverflow.com/questions/8252001/wp7-5e-chosenphoto-issue

 Solution 3:

public void DecodePhoto(byte[] byteVal)
        {
            if (byteVal == null) return ;

            try
            {
                MemoryStream strmImg = new MemoryStream(byteVal);
                BitmapImage myBitmapImage = new BitmapImage();
                myBitmapImage.BeginInit();
                myBitmapImage.StreamSource = strmImg;
                myBitmapImage.DecodePixelWidth = 200;
                myBitmapImage.EndInit();
                MugShot.Source = myBitmapImage;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
Source:
http://stackoverflow.com/questions/8897210/byte-to-bitmapimage
http://stackoverflow.com/questions/14718855/how-to-create-writeablebitmap-from-bitmapimage

Writable bit map SDK

https://www.blogger.com/blogger.g?blogID=8256225704949716108#editor/target=post;postID=5955005466364600781

http://stackoverflow.com/questions/5774038/resizing-resulting-camera-stream

Tuesday, April 1, 2014

BitMapImage into a byte array - XAML manipulation

I was messing with a listbox control for my windows phone app and needed to convert a series
of bitmap images.  Found this piece of code to convert the object.  Now I should be able to
use my httppost class to pass the image as a parameter.


public static byte[] ConvertToBytes(this BitmapImage bitmapImage) {    
          byte[] data = null;    
          using (MemoryStream stream = new MemoryStream()) {        
                      WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);        
                      wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);        
                      stream.Seek(0, SeekOrigin.Begin); data = stream.GetBuffer();    
           }    
return data;
}


Source:
http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array