Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

I am building a website Silverlight - PHP - Sql Server
and i need to save and retrieve images from the server.
Saving images from the Silverlight app works fine
and retrieving from the server to PHP also works
( i write the image to a file and it is fine)
but when i try to read from the PHP to the app
i get a few extra bytes and it's not recognised as an image.

the code is :

private void btnPhoto_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Random random = new Random();
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(download_OpenReadCompleted);
            Uri myUri = new Uri("http://servername/PHP/GetUserPhoto.php?userID=1" + "&sid=" + random.Next(), UriKind.RelativeOrAbsolute);
            webClient.OpenReadAsync(myUri, "GET");
        }
        void download_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if ((e.Cancelled != true) || (e.Result != null))
            {
                Stream stream = e.Result;
                BitmapImage loadedImage = new BitmapImage();
                stream.Position = 0;
                loadedImage.SetSource(e.Result);
                smallImage.Source = loadedImage;
            }
        } 

the error is on the line
loadedImage.SetSource(e.Result);

{System.Exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.BitmapSource_SetSource(BitmapSource bitmapSource, CValue& byteStream)
   at System.Windows.Media.Imaging.BitmapSource.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapImage.SetSourceInternal(Stream streamSource)
   at System.Windows.Media.Imaging.BitmapSource.SetSource(Stream streamSource)
   at SilverlightApplication13.MainPage.downloader_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)}


the stream i get back is 947 bytes - it should be 942 bytes

does anyone have any ideas what i am doing wrong




the php code is:

PHP
<?php
$spUserID = $_GET["userID"];

$serverName = "(local)";
$connectionOptions = array('Database'=>'dbname');

$conn = sqlsrv_connect( $serverName, $connectionOptions);

if( $conn === false )  {
    echo "error: " . sqlsrv_errors();
}

else {

    $tsql = "select imageData from images where userID = " . $spUserID;
    $stmt = sqlsrv_query($conn, $tsql );
    $result = sqlsrv_fetch( $stmt );
    $stream = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));

    $imgfile = fopen("UploadedImages/debug3.png", "w");
    while( !feof( $stream))
    {
        $str = fread( $stream, 2048);
        $str = fread( $stream, 2048);
        fwrite($imgfile, $str);
        echo $str;
    }
    fclose($imgfile);
}
sqlsrv_free_stmt( $strSql );
sqlsrv_close( $conn );
?>
Posted
Updated 1-Sep-10 18:24pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900