将图像从php加载到.net应用程序

时间:2013-12-15 21:47:05

标签: php vb.net httpwebrequest

我使用.htaccess在/ public_html / images / restricted中有几个图像 与此同时,我创建了一个获取图像的php文件。

使用POST更改特定ID之前的示例

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

如何创建我的Webrequest以从此php文件中提取请求的图像。

        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)

请帮助。我能够在类似的功能中拉出json数组和字符串,但我似乎无法检索图像。

2 个答案:

答案 0 :(得分:1)

我不是.NET方面的专家,所以我无法帮助,但不是使用:

print $file;

尝试使用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;
?>

答案 1 :(得分:1)

谷歌谷歌搜索几个小时后我就可以开始工作了。下面是我以前能够从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