1

I have several images in /public_html/images/ restricted using .htaccess Meanwhile I created a php file that gets an image.

Example before using POST to change specific ID

<?php>
$file = '/home/user/public_html/foodimage/ID-856-front.jpg';
header('Content-Type: image/jpeg');
print $file;
?>

How can I create my Webrequest to pull the image requested from this php file.

        Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
        request.Proxy = Nothing
        request.Method = "POST"
        Dim postData = postvalues
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

        Dim response As WebResponse = request.GetResponse()
        dataStream = response.GetResponseStream()

        Dim x As New BitmapImage
        x.StreamSource() = dataStream

        dataStream.Close()
        response.Close()
        Return (x)

Please Help. I able to pull off json arrays and strings in a similar function but I can't seem to retrieve images.

3
  • FTP is not an option. I've dealt with too many issues using ftp when not knowing a computers firewall settings. Commented Dec 16, 2013 at 2:53
  • Do you get the bytes? Then simply create an Image object and display it (winforms or wpf) or simply save it to the disk. Both options are well-documented and should be easy to find. Commented Dec 16, 2013 at 3:05
  • Can't read the length of the datastream so I can't tell if im getting anything in return. I'm using WPF Commented Dec 16, 2013 at 3:15

2 Answers 2

1

I'm not an expert in .NET, so I cannot help on that side, but instead of using:

print $file;

try using readfile() (http://www.php.net/readfile):

<?php>
$file = '/home/user/public_html/foodimage/ID-856-front.jpg';
header('Content-Type: image/jpeg');
readfile($file);
exit;
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Would like to ask as I'm a bit new to php. I tried print & readfile and it will show the image on the browser. Any info on the difference? Also I don't know how that info will get picked up using webrequest.
MattMc I would upvote your answer but I don't have enough reputation to do so. I appreciate your help thanks
1

Ok after googling for a couple of hours I was able to get it to work. Heres the code I used to be able to retrieve images from php.

php code

<?php>
$photoID = $_POST["uID"];
$file = "/home/username/public_html/imagefolder/ID-$photoID-front.jpg";
header('Content-Type: image/jpeg');
readfile($file);
exit;
?>

vb.net

    Public Function getimage(ByVal url As String, ByVal postvalues As String) As BitmapImage
    Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    request.Proxy = Nothing
    request.Method = "POST"
    Dim postData = postvalues
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()

    Dim x As New BitmapImage()
    Dim lsResponse As [String] = String.Empty
    Using lxResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Using reader As New BinaryReader(lxResponse.GetResponseStream())
            Dim lnByte As [Byte]() = reader.ReadBytes(1 * 1024 * 1024 * 10)

            Dim stream As New MemoryStream(lnByte)
            stream.Seek(0, SeekOrigin.Begin)
            x.BeginInit()
            x.StreamSource = stream
            x.EndInit()
        End Using
    End Using

    Return x
End Function

1 Comment

I think you could also determine Content_length: 1024 from server side, it may help. header('Content-length: '.filesize($file));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.