2009-07-28 183 views
0

因此,當我從DLL中提取圖像並將其放入圖像控件後,它是一個BitmapImage。要將它打包回dll,必須將其轉換回圖像。我怎樣才能將它轉換回圖像,我怎樣才能將它重新打包回dll?這全是用c#寫的wpf。將位圖轉換爲圖像

private void compileDLL_Click(object sender, RoutedEventArgs e) 
    { 
     string sourcePath = Directory.GetCurrentDirectory() + "\\PCAngelResources.dll"; 
     //destination path 
     string dllname = textBox1.Text + "_PCAngelResources.dll"; 
     string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
     string destFile = System.IO.Path.Combine(targetPath, dllname); 
     System.IO.File.Copy(sourcePath, destFile, true); 

     //lstImages = new Dictionary<string, Bitmap>(); 
     //string filename = "PCAngelResources.dll"; 
     Assembly pcangdll = Assembly.LoadFile(sourcePath); 
     System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture; 
     ResourceManager rm = new ResourceManager("PCAngelResources.DynResources", pcangdll); 
     rs = rm.GetResourceSet(culture, true, true); 
     ResourceWriter writer = new ResourceWriter(destFile); 

     foreach (DictionaryEntry resource in rs) 
     { 
      resources.Add((string)resource.Key); 
      if (resource.Key.Equals("Branding") || resource.Key.Equals("Advertising")) 
      { 
       if (resource.Key.Equals("Branding")) 
       { 
        writer.AddResource("Branding", image5.Source); 
        //System.Object obj = rm.GetObject((string)resource.Key); 
        //lstImages.Add((string)resource.Key, (Bitmap)obj); 
       } 
       else 
        if (resource.Key.Equals("Advertising")) 
        { 
         writer.AddResource("Advertising", image6.Source); 
        } 
      } 
     } 
     writer.Generate(); 
     System.Windows.MessageBox.Show("Done", "Process Finished", MessageBoxButton.OK, MessageBoxImage.Asterisk, MessageBoxResult.OK); 
    } 

當我做writer.Generate(),以使新的dll我得到以下錯誤: 型「System.Runtime.Serialization.SerializationException」未處理的異常出現在mscorlib.dll

其他信息:類型'System.Windows.Media.Imaging.BitmapFrameDecode'in Assembly'PresentationCore,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'未標記爲可序列化。

+0

一些示例代碼? – 2009-07-28 19:37:32

回答

1

我不知道「將它重新打包回DLL中」是什麼意思,但是有一種簡單的方法可以將WPF圖像從BitmapSource轉換回System.Drawing.Image。以下方法完成:

/// <summary> 
/// Converts a WPF bitmap to a System.Drawing.Bitmap 
/// </summary> 
/// <param name="wpfBitmap">BitmapSource to convert</param> 
/// <returns>A GDI Bitmap</returns> 
public static System.Drawing.Bitmap GdiBitmapFromWpfBitmap(BitmapSource wpfBitmap) 
{ 
    PngBitmapEncoder encoder = new PngBitmapEncoder(); 
    encoder.Frames.Add(BitmapFrame.Create(wpfBitmap)); 
    MemoryStream imageStream = new MemoryStream(); 
    encoder.Save(imageStream); 
    System.Drawing.Bitmap gdiBitmap = new System.Drawing.Bitmap(imageStream); 

    imageStream.Close(); 
    imageStream.Dispose(); 

    return gdiBitmap; 
}