2013-07-17 69 views
0

需要一些幫助。我有一個應用程序,我一起創建了兩張圖片。問題是當我試圖將位圖轉換爲bitmpaimage以在屏幕上顯示結果時。從我可以告訴,圖像不被保存到內存流在「NwImg.Save(內存,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圖像創建的位圖。我沒有讀過任何說它重要的東西,但我想我會通過那裏。

///這裏是要求大衛 //主要的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> 

// PIC類

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

你能張貼融合了圖像的代碼? –

+0

給我10分鐘, – chris

+0

在你的'apply_Click'中,在你創建一個位圖之前,你不要倒退'outStream'。這可能是一個問題。另外,我懷疑如果你在前面的'using'塊中放置'ResultPhoto.Source = Nwbi'行,事情就會開始起作用。只是一個猜測。我認爲在圖像實際從中加載之前,內存流將被丟棄。 –

回答

1

您將需要更改幾件事情,以便您的代碼可以工作。

  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. 內,您的產品圖類,更換這些線路

    //Save to current picture 
    Bitmap NewImage = new Bitmap(BottomImage.Width, BottomImage.Height, canvas); 
    return NewImage; 
    

    這個

    return BottomImage; 
    

    ,因爲你正在使用Bitmap類的過載,不創建一個新的位圖基於圖形對象,但只是其分辨率(這導致一個空的圖像)。因此,由於您繪製了BottomImage位圖,因此只需要返回該圖像。

enter image description here

+0

謝謝你這麼多大衛! !有用!!!!!我將不得不閱讀更多關於圖像的內容,我真的不知道從閱讀圖書館那裏知道圖像是什麼。再次感謝。 – chris

+0

這是一個很好的答案。 – Pogrindis

0

的所有代碼變量「內存」不可能在您評論爲「內存流時顯示爲空」的行上爲空因爲它明確分配在它上面的「使用」塊4行中。我猜你在調用方法中太早放置調試器斷點。試着把你的斷點放在你分配的行Nwbi.CacheOption,看看調試器是否告訴你你的預期。

+0

它是空的。我第一次運行它沒有休息和通知圖像沒有更新,然後我通過代碼倒退,並跑了好幾次,在這一點上,我失去了形象,Nwbi顯示空使用 – chris