如何保存<图片/>文件内容

本文关键字:文件 图片 何保存 保存 | 更新日期: 2023-09-27 18:02:47

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

    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"/>  

如何保存<图片/>文件内容

我认为你唯一的选择可以使用RenderTargetBitmap类,它从任何继承类类型如image的UIElement捕获图像。

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

HesamM,感谢您的回复。实际上我是这样做的:

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 **********");
    }