0

I have an image taken from my phone gallery, like below:

private void StackPanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
    PhotoChooserTask pct = new PhotoChooserTask();
    pct.Show();
    pct.Completed += pct_Completed;
}

void pct_Completed(object sender, PhotoResult e)
{
    BitmapImage img = new BitmapImage();

    if (e.ChosenPhoto != null)
    {
        img.SetSource(e.ChosenPhoto);
        imgphotochoser.Source = img;
    }
}

Now I want to save this image in a database, via a web service. So, I'm required to convert this image into a base64 string, but how can I do this?

I've tried this, but it throws an exception:

public string imagetobase64(image image,
  system.drawing.imaging.imageformat format)
{
    using (memorystream ms = new memorystream())
    {
        // convert image to byte[]
        image.save(ms, format);
        byte[] imagebytes = ms.toarray();

        // convert byte[] to base64 string
        string base64string = convert.tobase64string(imagebytes);
        return base64string;
    }
}
Pham
  • 33
  • 6
Mansinh
  • 1,365
  • 4
  • 19
  • 47
  • C# is a **case-sensitive** language – I4V Apr 26 '13 at 11:11
  • @DGibbs:The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?) – Mansinh Apr 26 '13 at 11:18
  • but which assembly? i dont know which essembly for drowing and it also give error in "save" – Mansinh Apr 26 '13 at 11:47
  • @DGibbs:when i am tring to add system.drowing dll at that time it will give error "a reference to a higher version or incompatible assembly cannot be added to the project" how can i solve problem – Mansinh Apr 29 '13 at 06:03

1 Answers1

5

Simply convert the byte[] to a base64 string:

byte[] bytearray = null;

using (MemoryStream ms = new MemoryStream())
{
    if (imgphotochoser.Source != null)
    {
        WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)imgphotochoser.Source);

        wbitmp.SaveJpeg(ms, 46, 38, 0, 100);
        bytearray = ms.ToArray();
    }
}
string str = Convert.ToBase64String(bytearray);

Base64 to byte[]:

byte[] fileBytes = Convert.FromBase64String(s);

using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
    ms.Write(fileBytes, 0, fileBytes.Length);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(ms);
    return bitmapImage;
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Mansinh
  • 417
  • 1
  • 6
  • 14
  • Can you please help me here http://stackoverflow.com/questions/23366212/convert-imagesource-to-base64string-wp8 – Goofy Apr 29 '14 at 14:25
  • I'm using this code to encode an image to save in a SQLite database, but when I try to decode, I get "FormatException", "Invalid length for a Base-64 char array or string" when trying to convert fromBase64String.... any ideia why? – Eggakin Baconwalker Nov 23 '15 at 17:02
  • How about convert image to 64 bit & covert 64 bit to image? – Eng Soon Cheah Dec 05 '16 at 13:18