none
How can I create simple button to save an image

    Question

  • Hello, How can I create simple button to save an image using print screen in my app C# windows store

    her is an example http://code.msdn.microsoft.com/CSWin8AppCropBitmap-52fa1ad7

    I want the same but I don't want the user to have the access to change the region

    I just want him to save it with out changing the region , because I will give a specific region for the image that will not be change

    thank u :)


    Friday, April 04, 2014 12:29 PM

Answers

  • I could be wrong but i believe it might work for you :)

    Use render target Bitmap 

    private async void Snapshot_Click(object sender, RoutedEventArgs e)
    {
        await CaptureAndSave(this);
    }
    
    async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CaptureAndSave(UIElement element)
    {
        // capture
        var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
        await bitmap.RenderAsync(element);
        var picker = new Windows.Storage.Pickers.FileSavePicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,
            FileTypeChoices = { { "JPEG", new[] { ".jpg" } } },
            SuggestedFileName = "Image",
            DefaultFileExtension = ".jpg",
            CommitButtonText = "Save",
        };
    
        // save
        var file = await picker.PickSaveFileAsync();
        using (var stream = await file.OpenStreamForWriteAsync())
        {
    
            var pixel = await bitmap.GetPixelsAsync();
            var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
            var random = stream.AsRandomAccessStream();
            var jpg = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;
            var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(jpg, random);
            var format = Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8;
            var alpha = Windows.Graphics.Imaging.BitmapAlphaMode.Ignore;
            var width = (uint)bitmap.PixelWidth;
            var height = (uint)bitmap.PixelHeight;
            encoder.SetPixelData(format, alpha, width, height, dpi, dpi, pixel.ToArray());
            await encoder.FlushAsync();
        }
        return file;
    }

    Regards , 

    hope it works for you


    Microsoft Student Partner

    Saturday, April 05, 2014 9:34 PM

All replies

  • You can use the SaveCroppedBitmapAsync method from that sample and pass in your own region. Just bypass the choose part of the sample.
    Friday, April 04, 2014 2:17 PM
    Owner
  • You can use the SaveCroppedBitmapAsync method from that sample and pass in your own region. Just bypass the choose part of the sample.

    Exactly , but I don't know how to do it , I'm a beginner
    Friday, April 04, 2014 3:54 PM
  • I could be wrong but i believe it might work for you :)

    Use render target Bitmap 

    private async void Snapshot_Click(object sender, RoutedEventArgs e)
    {
        await CaptureAndSave(this);
    }
    
    async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CaptureAndSave(UIElement element)
    {
        // capture
        var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
        await bitmap.RenderAsync(element);
        var picker = new Windows.Storage.Pickers.FileSavePicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,
            FileTypeChoices = { { "JPEG", new[] { ".jpg" } } },
            SuggestedFileName = "Image",
            DefaultFileExtension = ".jpg",
            CommitButtonText = "Save",
        };
    
        // save
        var file = await picker.PickSaveFileAsync();
        using (var stream = await file.OpenStreamForWriteAsync())
        {
    
            var pixel = await bitmap.GetPixelsAsync();
            var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
            var random = stream.AsRandomAccessStream();
            var jpg = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;
            var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(jpg, random);
            var format = Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8;
            var alpha = Windows.Graphics.Imaging.BitmapAlphaMode.Ignore;
            var width = (uint)bitmap.PixelWidth;
            var height = (uint)bitmap.PixelHeight;
            encoder.SetPixelData(format, alpha, width, height, dpi, dpi, pixel.ToArray());
            await encoder.FlushAsync();
        }
        return file;
    }

    Regards , 

    hope it works for you


    Microsoft Student Partner

    Saturday, April 05, 2014 9:34 PM
  • I could be wrong but i believe it might work for you :)

    Use render target Bitmap 

    private async void Snapshot_Click(object sender, RoutedEventArgs e)
    {
        await CaptureAndSave(this);
    }
    
    async System.Threading.Tasks.Task<Windows.Storage.StorageFile> CaptureAndSave(UIElement element)
    {
        // capture
        var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
        await bitmap.RenderAsync(element);
        var picker = new Windows.Storage.Pickers.FileSavePicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary,
            FileTypeChoices = { { "JPEG", new[] { ".jpg" } } },
            SuggestedFileName = "Image",
            DefaultFileExtension = ".jpg",
            CommitButtonText = "Save",
        };
    
        // save
        var file = await picker.PickSaveFileAsync();
        using (var stream = await file.OpenStreamForWriteAsync())
        {
    
            var pixel = await bitmap.GetPixelsAsync();
            var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
            var random = stream.AsRandomAccessStream();
            var jpg = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;
            var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(jpg, random);
            var format = Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8;
            var alpha = Windows.Graphics.Imaging.BitmapAlphaMode.Ignore;
            var width = (uint)bitmap.PixelWidth;
            var height = (uint)bitmap.PixelHeight;
            encoder.SetPixelData(format, alpha, width, height, dpi, dpi, pixel.ToArray());
            await encoder.FlushAsync();
        }
        return file;
    }

    Regards , 

    hope it works for you


    Microsoft Student Partner

    u r the best, thanks a lot :)

    small question again , if I want a specific part of the screen , how I do it ?

    I mean how should I do the points to take this position ?


    I don't need to print all screen, I only need part of it .

    thanks again :)

    • Proposed as answer by Bluxo Costa Wednesday, April 09, 2014 9:56 PM
    Sunday, April 06, 2014 10:13 PM