我正在尝试在Excel中制作水印,为此我创建了一个System.Drawing.Image并将其转换为C#中的System.Windows.Control图像 我的问题是,在代码之后,image.source,image为null。 我的代码:
MemoryStream ms = new MemoryStream();
imgWtrmrk.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
System.Windows.Controls.Image image = new
System.Windows.Controls.Image();
BitmapImage ix = new BitmapImage();
ix.BeginInit();
ix.CacheOption = BitmapCacheOption.OnLoad;
ix.StreamSource = ms;
ix.EndInit();
image.Source = ix;
我尝试过很多东西,但无法解决问题。 源不是null但我不能为它分配图像,
请帮助..
答案 0 :(得分:0)
Heyho,也许这就是你要找的东西?
///<summary>
/// Converts an System.Drawing.Image to an BitmapImage
///</summary>
///<param name="image">System.Drawing.Image</param>
///<param name="disposeImage">Image dispose</param>
///<param name="format">ImageFormat</param>
///<returns>BitmapImage or null</returns>
public static BitmapImage ImageToBitmapImage(Image image, bool disposeImage = true, ImageFormat format = null)
{
if (image != null)
{
if (format == null)
{
format = ImageFormat.Tiff;
}
using (MemoryStream ms = new MemoryStream())
{
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
image.Save(ms, format);
bmi.StreamSource = new MemoryStream(ms.ToArray());
bmi.EndInit();
bmi.Freeze();
if (disposeImage)
{
image.Dispose();
}
return bmi;
}
}
return null;
}
以下是将MemoryStream
保存到文件的方法。 (未经测试但应该有效)
///<summary>
/// Save an System.Drawing.Image to an file
///</summary>
///<param name="image">System.Drawing.Image</param>
///<param name="filePath">Physical path (C:\someimg.bmp)</param>
///<param name="disposeImage">Image dispose</param>
///<param name="format">ImageFormat</param>
///<returns>bool</returns>
public static bool ImageToFile(Image image, string filePath, bool disposeImage = true, ImageFormat format = null)
{
if (image != null)
{
if (format == null)
{
format = ImageFormat.Tiff;
}
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
{
image.Save(fs, format);
}
if (disposeImage)
{
image.Dispose();
}
if (File.Exists(filePath))
{
return true;
}
}
return false;
}
实现看起来像(XAML):
<Window x:Class="Multi_Explorer.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Width="50" Height="50" Source="{Binding BitmapImg}"/>
<!-- or -->
<ContentPresenter Grid.Column="1" Width="50" Height="50" Content="{Binding Img}"/>
</Grid>
</Window>
代码背后:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
BitmapImg = ImageToBitmapImage(System.Drawing.Image.FromFile(@"C:\some.img"));
Img = new System.Windows.Controls.Image()
{
Source = BitmapImg
};
DataContext = this;
}
public System.Windows.Controls.Image Img
{
get; set;
}
public BitmapImage BitmapImg
{
get; set;
}
}
此致
k1ll3r8e
答案 1 :(得分:0)
System.Windows.Control 图像到 System.Drawing.Bitmap
public System.Drawing.Bitmap TakePhoto()
{
var bmpImage = (System.Windows.Media.Imaging.BitmapImage)base.Source;
var bmpFrame = System.Windows.Media.Imaging.BitmapFrame.Create(bmpImage);
var bmpEncoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
bmpEncoder.Frames.Add(bmpFrame);
var ms = new System.IO.MemoryStream();
bmpEncoder.Save(ms);
return new System.Drawing.Bitmap(ms);
}