开发者

Silverlight 4 image upload problem

I am using Silverlight4 with java webervices in jsp page. I want to save an image to the server so trying to do this with java webservice. I am using below lines of code but output is damaged. I dont understand why. Please help me. This is really important for me. When i try to open 3mb jpeg file contains "Windows Photo Viewer cant open this picture because file appears to be damaged, corrupted or is too large."

Client Side COde

WriteableBitmap wb = new WriteableBitmap(bitmapImage);
            byte[] bb = ToByteArray(wb);

pub开发者_如何学Pythonlic byte[] ToByteArray(WriteableBitmap bmp)
    {
        int[] p = bmp.Pixels;
        int len = p.Length * 4;
        byte[] result = new byte[len]; // ARGB
        Buffer.BlockCopy(p, 0, result, 0, len);
        return result;
    }

WebService Code

@WebMethod(operationName = "saveImage")
public Boolean saveImage(@WebParam(name = "img")
byte[] img, @WebParam(name = "path")
String path) {
    try{
        FileOutputStream fos = new FileOutputStream("C:\\Users\\TheIntersect\\Desktop\\sharp_serializer_dll\\saved.jpg");
        fos.write(img);
        fos.close();
        return true;
    }
    catch(Exception e){
        return false;
    }
}


I found my answer on forums.silverlight.net It is very interesting when i try to call ReadFully(Stream) just after the Stream definition it works but when i call 10 lines of code later it returns all 0. FUnction

        public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[input.Length];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

Fail Code

using (Stream str = opd.File.OpenRead()) 
        {  
            BitmapImage bitmapImage = new BitmapImage(); 
            bitmapImage.SetSource(str); 
            image.Tag = bitmapImage.UriSource.ToString(); 
            image.Source = bitmapImage; 
            image.Width = width; 
            image.Height = height; 
            image.Stretch = Stretch.Uniform; 
            container.Child = image; 
            rtb.Selection.Insert(container); 
            ServiceReference1.webWordWebServiceClient s = new ServiceReference1.webWordWebServiceClient(); 
            byte[] bb = ReadFully(str); 
            s.saveImageCompleted += new EventHandler<ServiceReference1.saveImageCompletedEventArgs>(s_saveImageCompleted); 
            s.saveImageAsync(bb, "gungorrrr"); 

        }

Successfull Code

using (Stream str = opd.File.OpenRead()) 
        { 
            byte[] bb = ReadFully(str); 
            BitmapImage bitmapImage = new BitmapImage(); 
            bitmapImage.SetSource(str); 
            image.Tag = bitmapImage.UriSource.ToString(); 
            image.Source = bitmapImage; 
            image.Width = width; 
            image.Height = height; 
            image.Stretch = Stretch.Uniform; 
            container.Child = image; 
            rtb.Selection.Insert(container); 
            ServiceReference1.webWordWebServiceClient s = new ServiceReference1.webWordWebServiceClient(); 

(bitmapImage);

            s.saveImageCompleted += new EventHandler<ServiceReference1.saveImageCompletedEventArgs>(s_saveImageCompleted); 
            s.saveImageAsync(bb, "gungorrrr"); 

        }

Link: http://forums.silverlight.net/forums/p/234126/576070.aspx#576070

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜