0

尝试使用 Windows Store 示例将图像捕获到 OneNote API 被证明有点困难。

我得到的是“红色 x”而不是图像。看起来图像源不正确,但我做了一些更改。

以下是我的代码:(任何帮助将不胜感激)

private async void btnCreatePage_Click(object sender, RoutedEventArgs e)
        {
            await CreatePageWithImage();
        }


 private async Task CreatePageWithImage()
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            if (IsAuthenticated)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authClient.Session.AccessToken);
            }

            const string imagePartName = "assets\\Asthma.jpg";
            string date = GetDate();
            string simpleHtml = "<html>" +
                                "<head>" +
                                "<title>A simple page created with an image on it</title>" +
                                "<meta name=\"created\" content=\"" + date + "\" />" +
                                "</head>" +
                                "<body>" +
                                "<h1>This is a page with an ximage on it</h1>" +
                                "<img src=\"name:" + imagePartName + "\" alt=\"A beautiful logo\" width=\"426\" height=\"68\" />" +
                                "</body>" +
                                "</html>";

       // Create the image part - make sure it is disposed after we've sent the message in order to close the stream.
            HttpResponseMessage response;

    using (var imageContent = new StreamContent(await GetBinaryStream("assets\\Asthma.jpg")))
            {
                imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Post, PagesEndPoint)
                {
                    Content = new MultipartFormDataContent
                    {
                        {new StringContent(simpleHtml, System.Text.Encoding.UTF8, "text/html"), "Presentation"},
                        {imageContent, imagePartName}
                    }
                };
                // Must send the request within the using block, or the image stream will have been disposed.
                response = await client.SendAsync(createMessage);
            }
            tbResponse.Text = response.ToString();
        }
4

1 回答 1

-1

在您的代码中,您已将媒体类型设置为“image/jpg”。这应该设置为“image/jpeg”。您还需要更改 imagePartName 以删除“\”。

——詹姆斯(@jmslau

于 2014-08-01T00:25:33.880 回答