尝试使用 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();
}