这里需要一点帮助。我有一个正在创建的应用程序,它将两张图片合并在一起。问题是当我试图将位图转换为 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;
}
}
}
}