繁体   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