The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

[UWP]How to save image to azure mysql database using UWP RRS feed

  • Question

  • I have captured picture using cameracaptureui and I have the image in image control. Now the problem is how to save this captured image to my database.? normally i did this task in windows form via converting image to byets. but now bit confused in UWP. thanks in advance       
    private async void button_Copy_Click(object sender, RoutedEventArgs e)
                //create camera instance with camera capture ui 
                CameraCaptureUI captureUI = new CameraCaptureUI();
                captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
                captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
                StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
                if (photo == null)
                    // User cancelled photo capture
                //return the captured results to fram via bitmap
                IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
                SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);
                imageControl.Source = bitmapSource;

    Tuesday, June 28, 2016 6:10 AM


  • You can do that by converting the image to a Base64 string and put that in the database. I have a helper class for that with a number of methods. This code is based of a very good blogpost of Jerry Nixon.

    Add these usings to the class where you add these methods: System.IO, Windows.UI.Xaml.Media, System.Runtime.InteropServices.WindowsRuntime and Windows.UI.Xaml.Media.Imaging. In my case I've implemented a static class called FileService with static methods.

    public static class FileService
        public async static Task<StorageFile> OpenImageFile()
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            // Dropdown of file types the user can save the file as
            return await openPicker.PickSingleFileAsync();
        private static async Task<string> ToBase64(StorageFile bitmap)
            var stream = await bitmap.OpenAsync(Windows.Storage.FileAccessMode.Read);
            var decoder = await BitmapDecoder.CreateAsync(stream);
            var pixels = await decoder.GetPixelDataAsync();
            var bytes = pixels.DetachPixelData();
            return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
        private static async Task<string> ToBase64(RenderTargetBitmap bitmap)
            var bytes = (await bitmap.GetPixelsAsync()).ToArray();
            return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
        private static async Task<string> ToBase64(byte[] image, uint height, uint width, double dpiX = 96, double dpiY = 96)
            // encode image
            var encoded = new InMemoryRandomAccessStream();
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, encoded);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, height, width, dpiX, dpiY, image);
            await encoder.FlushAsync();
            // read bytes
            var bytes = new byte[encoded.Size];
            await encoded.AsStream().ReadAsync(bytes, 0, bytes.Length);
            // create base64
            return Convert.ToBase64String(bytes);
        public async static Task<string> ConvertFileToBase64(StorageFile file)
            var stream = await file.OpenAsync(FileAccessMode.Read);
            var decoder = await BitmapDecoder.CreateAsync(stream);
            var pixels = await decoder.GetPixelDataAsync();
            var bytes = pixels.DetachPixelData();
            return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
        public static async Task<ImageSource> FromBase64(string base64)
            // read stream
            var bytes = Convert.FromBase64String(base64);
            var image = bytes.AsBuffer().AsStream().AsRandomAccessStream();
            // decode image
            var decoder = await BitmapDecoder.CreateAsync(image);
            // create bitmap
            var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
            await output.SetSourceAsync(image);
            return output;

    I hope this helps.

    Martin Tirion - UX Evangelist @ Microsoft <If this answers your question, please mark this as answer>

    Tuesday, June 28, 2016 10:10 AM