0

我有一个实用程序,除了上传文件的另一个选项外,它还允许用户拍摄相机照片并上传。我已经完成了大部分工作,除了我必须在上传之前将网络摄像头图像转换为 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 所需的字节时做错了什么?

4

2 回答 2

1

很高兴看到你解决了,

这是我的运行代码,我使用 png 格式,还有文件大小检查。也许它可以帮助别人。

dSrvPR 是我的域服务类实例

photo 是我的 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! :)";
            }

        });
于 2012-05-02T07:16:09.063 回答
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
}
于 2012-05-01T13:40:14.340 回答