简体   繁体   中英

bitmap not saving image to memory stream

Need a little help here. I have a app that I am creating that mergies two pictures together. The issue is when i am trying to convert the bitmap into a bitmpaimage to display the result on the screen. from what i can tell, the image is not being save to the memory stream at "NwImg.Save(memory,ImageFormat.Jpeg);" Any ideas??

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

            }

I don't know if this matter but NwImg represents a bitmap that was created by merging a png image on top of a jpeg. I didn't read anything that said it matter but i figured i would through that in there.

/// here is all the code as requested 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;
        }
    }
}

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

// pic class

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;
            }
        }
    }
}

You will need to change a couple of things so that your code can work.

  1. Use the following code for the Bitmap to BitmapImage conversion.

     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. Inside your Pic class, replace these lines

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

    to this

     return BottomImage; 

    since the overload of the Bitmap class that you are using, doesn't create a new bitmap based on the Graphics object but just its resolution (this results to an empty image). So, since you draw onto the BottomImage bitmap, you just need to return that image.

在此处输入图片说明

The variable "memory" cannot possibly be null on the line where you commented "memory stream is showing null" when that line executes because it is definitely assigned in the "using" block 4 lines above it. I'm guessing you put a debugger breakpoint too early in the method. Try putting your breakpoint on the line where you assign Nwbi.CacheOption and see if the debugger tells you what you expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM