簡體   English   中英

位圖不將圖像保存到內存流

[英]bitmap not saving image to memory stream

這里需要一點幫助。 我有一個正在創建的應用程序,它將兩個圖片融合在一起。 問題是當我嘗試將位圖轉換為bitmpaimage以在屏幕上顯示結果時。 據我所知,圖像未保存到“ NwImg.Save(memory,ImageFormat.Jpeg);”處的內存流中。 有任何想法嗎??

 //The Code   
    //bitmap to bitmapimage conversion
            using (MemoryStream memory = new MemoryStream())
            {//NwImg is type Bitmap, and at this point i checked properties and values did copy over from the merging
                NwImg.Save(memory, ImageFormat.Jpeg);//here image NwImg.save is suppose to transfer to memory
                memory.Position = 0;
                Nwbi.StreamSource = memory;//memory stream is showing null
                Nwbi.CacheOption = BitmapCacheOption.OnLoad;

            }

我不知道這是否重要,但NwImg表示通過將png圖像合並到jpeg頂部而創建的位圖。 我什么也沒讀,但我想我會通過那兒。

///這是請求的所有代碼david // Main c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;


namespace PicMerger2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Pic currentPic = new Pic();
        BitmapImage bi = new BitmapImage(new Uri("\\original photo.jpg"));
        BitmapImage Nwbi = new BitmapImage();
        public MainWindow()
        {
            InitializeComponent();

            OriginalPhoto.Source = bi;
            ResultPhoto.Source = Nwbi;

        }

        private void apply_Click(object sender, RoutedEventArgs e)
        {
            Bitmap NwImg;
            //bitmapimage to bitmap conversion
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bi));
                enc.Save(outStream);
                System.Drawing.Bitmap MarkThisPic = new System.Drawing.Bitmap(outStream);

                // return bitmap; <-- leads to problems, stream is closed/closing ...
                NwImg = new Bitmap(MarkThisPic);
            }
            NwImg = currentPic.MergerTheseTwo(NwImg);


            //bitmap to bitmapimage conversion
            using (MemoryStream memory = new MemoryStream())
            {
                NwImg.Save(memory, ImageFormat.Jpeg);
                memory.Position = 0;
                Nwbi.StreamSource = memory;
                Nwbi.CacheOption = BitmapCacheOption.OnLoad;

            }
            ResultPhoto.Source = Nwbi;
        }
    }
}

//主xaml

<Window x:Class="PicMerger2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid HorizontalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <StackPanel>
                <Image x:Name="OriginalPhoto" Height="200" Stretch="UniformToFill" Source="{Binding}"></Image>
                <Label>Original Images</Label>
            </StackPanel>
            <Button x:Name="apply" Click="apply_Click" Height="25" >Apply Watermark</Button>
            <StackPanel>
                <Image x:Name="ResultPhoto" Height="200" Stretch="UniformToFill" Source="{Binding}"></Image>
                <Label>Watermarked Image</Label>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

//圖片類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace PicMerger2
{
    class Pic
    {
        Bitmap Watermark = new Bitmap(PicMerger2.Properties.Resources._Watermark);


        public Bitmap MergerTheseTwo(Bitmap BottomImage)
        {
            try
            {
                using (var canvas = Graphics.FromImage(BottomImage))
                {
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    //       canvas.DrawImage(BottomImage, new Rectangle(0, 0, BottomImage.Width, BottomImage.Height), new Rectangle(0, 0, BottomImage.Width, BottomImage.Height), GraphicsUnit.Pixel);
                    canvas.DrawImage(Watermark, 0, 0);
                    canvas.Save();

                    //Save to current picture
                    Bitmap NewImage = new Bitmap(BottomImage.Width, BottomImage.Height, canvas);
                    return NewImage;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

您將需要進行一些更改,以便您的代碼可以正常工作。

  1. 使用以下代碼將位圖轉換為BitmapImage。

     using (MemoryStream memory = new MemoryStream()) { NwImg.Save(memory, ImageFormat.Jpeg); memory.Position = 0; Nwbi = new BitmapImage(); Nwbi.BeginInit(); Nwbi.StreamSource = memory; Nwbi.CacheOption = BitmapCacheOption.OnLoad; Nwbi.EndInit(); } 
  2. 在您的Pic類中,替換這些行

     //Save to current picture Bitmap NewImage = new Bitmap(BottomImage.Width, BottomImage.Height, canvas); return NewImage; 

    對此

     return BottomImage; 

    由於您正在使用的Bitmap類的重載,因此不會基於Graphics對象而是根據其分辨率來創建新的位圖(這將導致生成空圖像)。 因此,由於您繪制了BottomImage位圖,因此只需要返回該圖像即可。

在此處輸入圖片說明

執行該行時,在注釋“內存流顯示為空”的行上,變量“ memory”不可能為空,因為該變量肯定是在其上方的“ using”塊的第4行中分配的。 我猜想您在該方法中放置調試器斷點為時過早。 嘗試將斷點放在分配Nwbi.CacheOption的行上,看看調試器是否告訴您您的期望。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM