简体   繁体   中英

c# System.Windows.Control Image to a .bmp file?

I am trying to make watermark in Excel, For that I have create a System.Drawing.Image and converted it to System.Windows.Control Image in c# My problem is , after code the image.source, image is null. My code:

        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;

I have tried a lot of things but couldn't solve the problem. the source is not null but I couldnt assign it the image,

Please help..

Heyho, maybe this is what you're looking for?

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

Here is a method to save the MemoryStream to an file. (Untested but it should work)

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

The implementation could look like (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>

Code behind:

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

Regards,

k1ll3r8e

System.Windows.Control Image to a 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);
}

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