需要帮助将图像从Silverlight4传递到COM。
我正在尝试从WritableBitmap传递一个ByteArray并将其转换回Bitmap。
//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 :(得分: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);
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
我终于搞定了......将Bitmap从Silverlight传递给Silverlight COM Wrapper。
//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