locked
How to save Windows::UI::Xaml::Controls::Image^ to the .jpg or png file (C++/Cx Win 8/WinRT)?

    General discussion

  • I want to save the Photo (Windows::UI::Xaml::Controls::Image^) to the .jpg or .png file. (Using C++/Cx)

    I always searched the solution about C#. It is useless for me.

    My working condition is the Win8 / C++/CX.

     
    Thursday, January 16, 2014 3:48 AM

All replies

  • What is the source of your photo?

    You cannot extract the bits from an Image control (on Windows 8.1 you can get close with RenderTargetBitmap). You will need to get the bitmap from the Image's source. If it is a file (directly or via a BitmapImage) then the easiest way is to copy it directly. If it is a WriteableBitmap then you can extract the PixelBuffer and run it through a BitmapEncoder.

    If you search the sample gallery for BitmapEncoder you'll find it demonstrated in several C++ samples.

    --Rob

    Thursday, January 16, 2014 4:01 AM
    Moderator
  • The Photo is built by DirectX.(SurfaceImageSource) Look below.

    Code:

     gDirectXImageDraw = ref new DirectXImageSource(  g_WSize, g_HSize , true ); //Inherit SurfaceImageSource
     ImageBrush^ Brush = ref new ImageBrush();
     Brush->ImageSource = gDirectXImageDraw;
     DXView->Source = gDirectXImageDraw; //My Photo

    When I finished plotting the 3D model, I want to save the .jpg file.

    However , The photo is in Windows::UI::Xaml::Controls::Image^.

    I dont't known how to save the .jpg file.

    Similar this example below:

    http://code.msdn.microsoft.com/windowsapps/XAML-SurfaceImageSource-58f7e4d5 




    • Edited by KC-Steven Thursday, January 16, 2014 8:33 AM
    Thursday, January 16, 2014 6:28 AM
  • This Code come from http://www.codeproject.com/Messages/2987941/How-to-save-System-Windows-Control-Image-to-a-bmp-.aspx

    But It is C#.

    obj is type of "System.Windows.Controls.Image"

    System.Windows.Controls.Image myImage = ((Image)obj); System.Windows.Media.Imaging.BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage(); bitmapImage = ((System.Windows.Media.Imaging.BitmapImage)myImage.Source); System.Windows.Media.Imaging.PngBitmapEncoder pngBitmapEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder.PngBitmapEncoder(); System.IO.FileStream stream = new System.IO.FileStream(@"C:\newImage.png", FileMode.Create); pngBitmapEncoder.Interlace = PngInterlaceOption.On; pngBitmapEncoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapImage)); pngBitmapEncoder.Save(stream); stream.Flush(); stream.Close();


    • Edited by KC-Steven Thursday, January 16, 2014 6:36 AM
    Thursday, January 16, 2014 6:36 AM
  • This isn't a C# vs. C++ problem: the code you have is for WPF and isn't relevant to Windows Store apps in any language.

    You will need to save out your image at the DirectX level rather than from the Xaml level. Take a look at the DirectXTex library's CaptureTexture and SaveToXXXFile methods.

    --Rob

    Thursday, January 16, 2014 8:22 PM
    Moderator
  • Thank you very much!! I search information that you provide at once. 
    Friday, January 17, 2014 12:51 AM