Silverlight / ImageTools将Webcam Imagesource转换为Jpeg不起作用

时间:2012-05-01 00:38:47

标签: silverlight image

我有一个实用程序,允许用户拍摄相机照片并上传,以及上传文件的其他选项。除了我必须在上传之前将网络摄像头图像转换为jpg之外,我已经完成了大部分工作。下面的代码没有错误,但会产生无效的图像数据:

void CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
    busyIndicator.IsBusy = true;
    stopCapture();
    capturedImage.ImageSource = e.Result;

    ImageTools.ExtendedImage eimg = e.Result.ToImage();
    var encoder = new ImageTools.IO.Jpeg.JpegEncoder();

    Stream stream = eimg.ToStreamByExtension("jpg");

    //DO THIS LATER
    //if (stream.Length > 512000)
    //{
    //    eimg = ExtendedImage.Resize(eimg, 240, new NearestNeighborResizer());
    //    stream = eimg.ToStreamByExtension("jpg");
    //}

    encoder.Encode(eimg, stream);

    BitmapImage bitmap = new BitmapImage();
    bitmap.SetSource(stream);

    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, (int)stream.Length);

    // picture file a class object to be used by uploader
    pictureFile.PictureName = "webcam.jpg";  // name will be changed later
    pictureFile.PictureStream = bytes;


    HtmlPage.Window.Invoke("gotDetails_WebCam", "");  // post page, then come back and do upload
}

以下是PictureFile的样子:

[DataContract]
public class PictureFile
{
    [DataMember]
    public string PictureName { get; set; }

    [DataMember]
    public byte[] PictureStream { get; set; }
}

有人可以弄清楚我做错了什么来生成jpeg所需的字节?

2 个答案:

答案 0 :(得分:1)

很高兴看到你解决了,

这是我正在运行的代码, 我使用png格式,还有文件大小检查。 也许它对另一个人有帮助。

dSrvPR是我的域服务类实例

照片是我的EF中的实体对象。

_captureSource.CaptureImageCompleted += ((s, args) =>
        {
            if (dSrvPR.PR_PATIENTPHOTOs.Count > 0 && photo != null)
            {
                dSrvPR.PR_PATIENTPHOTOs.Remove(photo);
            }

            dSrvPR.PR_PATIENTPHOTOs.Clear();

            photo = new PR_PATIENTPHOTO();

            ImageTools.ExtendedImage eimg=args.Result.ToImage();
            var encoder=new ImageTools.IO.Png.PngEncoder();                                 

            Stream stream= eimg.ToStreamByExtension("png");
            if (stream.Length > 512000)
            {                    
                eimg= ExtendedImage.Resize(eimg, 240, new NearestNeighborResizer());                    
                stream = eimg.ToStreamByExtension("png");
            }                     
            if (stream.Length <= 512001)
            {
                BinaryReader binary = new BinaryReader(stream);
                //Read bytes from the BinaryReader and put them into a byte array.
                Byte[] file = binary.ReadBytes((int)stream.Length);
                photo.ID = Guid.NewGuid();
                photo.PHOTO = file;
                photo.PHOTODATE = DateTime.Now;
                photo.ISACTIVE = true;
                //some more unrelated fields
                dSrvPR.PR_PATIENTPHOTOs.Add(photo);
                dSrvPR.SubmitChanges();
  //Msg succedded
            }
            else
            {
                Util.alert(...,"file size exceeded! :)";
            }

        });

答案 1 :(得分:0)

我的错误。看来我在那里有一些额外的代码(不必要地将流转换为位图)。这就是我的工作:

void CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
    busyIndicator.IsBusy = true;
    stopCapture();
    capturedImage.ImageSource = e.Result;

    ImageTools.ExtendedImage eimg = e.Result.ToImage();
    var encoder = new ImageTools.IO.Jpeg.JpegEncoder();

    Stream stream = eimg.ToStreamByExtension("jpg");

    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, (int)stream.Length);

    // picture file a class object to be used by uploader
    pictureFile.PictureName = "webcam.jpg";  // name will be changed later
    pictureFile.PictureStream = bytes;


    HtmlPage.Window.Invoke("gotDetails_WebCam", "");  // post page, then come back and do upload
}