如何将<img/>内容保存到文件中

时间:2016-08-16 16:49:58

标签: c# windows-phone-8.1 save-image

我必须将Image标记中的内容保存到文件中。我使用了很多谷歌叔叔,但他不知道:(

    private void QRbutton_Click(object sender, RoutedEventArgs e)
    {

        IBarcodeWriter writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new ZXing.Common.EncodingOptions
            {
                Height = 1200,
                Width = 1200
            }
        };
        String message = "";
        if (String.IsNullOrEmpty(QRtxt.Text))
        {
            message = "You send empty message. ";
        }
        else
        {
            //Saving Input to string 
            message = QRtxt.Text;
            StckPnlProfile_Layout.Visibility = Visibility.Collapsed;
            QRsendbtn.Visibility = Visibility.Visible;

        }
        var result = writer.Write(message);
        var wb = result.ToBitmap() as WriteableBitmap;
        System.Diagnostics.Debug.WriteLine("BEFORE[Saving image to file]");
        QRimage.Source = wb; 
    }

还有Image的XAML代码:

    <Image x:Name="QRimage" Height="300"/>  

2 个答案:

答案 0 :(得分:0)

我认为您唯一的选择是使用RenderTargetBitmap类,它从任何UIElement继承的类类型中捕获图像,如Image。

https://msdn.microsoft.com/library/windows/apps/xaml/windows.ui.xaml.media.imaging.rendertargetbitmap.aspx

答案 1 :(得分:0)

Hesa​​mM,谢谢你的回复。其实我是这样做的:

private async void saveImage(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("********** Function STARTED: Save QR code as image to jpg **********");

        try
        {
            System.Diagnostics.Debug.WriteLine("Searching for assets folder.");
            var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder localFolder = await package.GetFolderAsync("Assets");
            StorageFile file = await localFolder.CreateFileAsync("savedimage.jpg", CreationCollisionOption.ReplaceExisting);
            var renderTargetBitmap = new RenderTargetBitmap();
            System.Diagnostics.Debug.WriteLine("Rendering an image.");

            await renderTargetBitmap.RenderAsync(QRimage);
            var pixels = await renderTargetBitmap.GetPixelsAsync();

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                System.Diagnostics.Debug.WriteLine("Save QR code to jpg.");

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                byte[] bytes = pixels.ToArray();
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    DisplayInformation.GetForCurrentView().LogicalDpi,
                    bytes);

                await encoder.FlushAsync();
            }
            MessageDialog SuccessMsg = new MessageDialog("Code QR saved.");
            await SuccessMsg.ShowAsync();
        }
        catch (Exception ex)
        {
            //MessageDialog ErrMsg = new MessageDialog("Error Ocuured!");  
            System.Diagnostics.Debug.WriteLine("ERROR ZAPISU PLIKU: " + ex);


        }
        Debug.WriteLine("********** Function STOPPED: Save QR code as image to jpg **********");

    }