2011-12-06 56 views
0

將圖像從Silverlight4傳遞到COM需要幫助。Silverlight - 將WritableBitmap圖像傳遞給COM並轉換爲位圖

我試圖通過從WritableBitmap ByteArray並將其轉換回位圖。

//In silverlight 4: 
public string func1() 
{ 
    WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)imgTempCropped.Source); 
    byte[] imgbytes = ToByteArray(bitmap); 
    dynamic comClass = AutomationFactory.CreateObject("OCRLibrary.OCRClass"); 
    ocrText = comClass.Process(imgbytes); 
} 

//In COM: 
public string Process(byte []imgbytes) 
{ 
    Stream input = new MemoryStream(imgbytes); 

    try{ 
     Bitmap bitmap1 = new Bitmap(input); 
    }catch(Exception e) 
    { 
     return e.Message; 
    } 
} 

//錯誤消息: 參數無效。

我甚至嘗試傳遞一個Base64String,但同樣的錯誤信息被拋出:(

回答

0

我終於得到它的工作...將Silverlight的位圖傳遞給Silverlight COM包裝器

//In Silverlight: 

dynamic comClass = AutomationFactory.CreateObject("OCRLibrary.OCRClass"); 
WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)imgTempCropped.Source); 
byte[] imgbytes = ToByteArrayOptimized(bitmap);          
ocrText = comClass.Process(imgbytes); 


//Found this for ImageTools: ImageTools.IO.Jpeg.JpegEncode 
//using ImageTools; 
//using ImageTools.Helpers; 
//using ImageTools.IO; 
//using ImageTools.IO.Bmp; 
//using ImageTools.IO.Png; 
//using ImageTools.IO.Jpeg; 
//I have yet to remove couple of using from here since I added all to test the code ;) 

    #region To byte array (optimized) 

    /// <summary> 
    /// Synchronously converts a bitmap to a byte array. 
    /// The used format can be JPEG or PNG, depending on which one 
    /// results in a smaller file size. 
    /// </summary> 
    /// <param name="bitmap">The bitmap to encode.</param> 
    /// <returns>The encoded image either in PNG or JPEG format.</returns> 
    public byte[] ToByteArrayOptimized(WriteableBitmap bitmap) 
    { 
     ExtendedImage image = bitmap.ToImage(); 

     // encode to jpeg 
     MemoryStream jpegStream = new MemoryStream(); 
     _jpegEncoder.Encode(image, jpegStream); 

     // encode to png 
     // MemoryStream pngStream = new MemoryStream(); 
     // _pngEncoder.Encode(image, pngStream); 

     // decide which one we should use 
     // MemoryStream formatToUse = jpegStream.Length < pngStream.Length ? jpegStream : pngStream; 
     MemoryStream formatToUse = jpegStream; 
     byte[] result = formatToUse.ToArray(); 

     // done 
     return result; 
    } 

    //In COM: 
    [ComVisible(true)] 
    //public string Process(string base64string) 
    public string Process(byte[] imgbytes) 
    { 
     MemoryStream mystream = new MemoryStream(imgbytes); 
     System.Drawing.Image p = System.Drawing.Image.FromStream(mystream); 
     Bitmap Img = new Bitmap(p);  // :) I got Bitmap here.  

    } 

http://www.pitorque.de/MisterGoodcat/post/Improving-the-image-upload-sample.aspx

0

什麼是COM ???

private void SaveToIsolatedStorage(Stream imageStream, string fileName, byte[] arr) 
     { 



      try 
      { 

       using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (myIsolatedStorage.FileExists(fileName)) 
        { 
         myIsolatedStorage.DeleteFile(fileName); 
        } 
        myIsolatedStorage.CreateDirectory("Album"); 
        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName); 
        fileStream.Write(arr, 0, arr.Length); 
        fileStream.Close(); 
        return; 
        BitmapImage bitmap = new BitmapImage(); 
        bitmap.SetSource(imageStream); 

        WriteableBitmap wb = new WriteableBitmap(bitmap); 
        wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
        fileStream.Close(); 

       } 
      } 
      catch (Exception ex) { } 

     } 

Stream uc = new MemoryStream(img); 


       SaveToIsolatedStorage(uc, tempJPEG, img); 
+0

嗨RAED,我在Silverlight和EmguCV建設OCR Applicatin。由於我想讓應用程序工作在脫機模式下,因此我在COM Wrapper中添加了帶有OCR相關代碼的C#代碼。這使我可以使用Silverlight的「瀏覽器外」,並在其中嵌入非Silverlight代碼:)。 – Kaps

0
check it 

public string func1() 
{ 
    WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)imgTempCropped.Source); 
    byte[] imgbytes = ToByteArray(bitmap); 
    dynamic comClass = AutomationFactory.CreateObject("OCRLibrary.OCRClass"); 
    ocrText = comClass.Process(imgbytes); 
} 

//In COM: 
public string Process(byte []imgbytes) 
{ 
    Stream input = new MemoryStream(imgbytes); 
    input.Write(arr, 0, arr.Length); 
    input.Close(); 
    try{ 
     Bitmap bitmap1 = new Bitmap(input); 
    }catch(Exception e) 
    { 
     return e.Message; 
    } 
} 
+0

嗨,我試過了: Stream input = new MemoryStream(imgbytes); input.Write(imgbytes,0,imgbytes.Length); input.Close(); 嘗試 位圖bitmap1 =新的位圖(輸入); } 趕上(例外五) { 返回e.Message; } 錯誤消息是「無法訪問已關閉的流」。所以我評論了Close,並得到了新的錯誤信息,因爲「參數無效」。 – Kaps