0

这里需要一点帮助。我有一个正在创建的应用程序,它将两张图片合并在一起。问题是当我试图将位图转换为 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 表示通过在 jpeg 顶部合并 png 图像创建的位图。我没有读到任何说这很重要的东西,但我想我会在那里完成。

/// 这里是所要求的所有代码 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;
        }
    }
}

//主xml

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

2 回答 2

1

您将需要更改一些内容,以便您的代码可以工作。

  1. 使用以下代码进行 Bitmap 到 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 位图上绘图,您只需要返回该图像。

在此处输入图像描述

于 2013-07-18T13:55:46.603 回答
0

当该行执行时,变量“memory”在您注释“内存流显示为空”的行上不可能为空,因为它肯定是在它上面的“使用”块 4 行中分配的。我猜你在方法中太早地放置了一个调试器断点。尝试将断点放在您分配的行上Nwbi.CacheOption,看看调试器是否告诉您您的预期。

于 2013-07-18T01:54:16.777 回答