读取同一图像流两次
Reading the Same Image Stream Twice
我想获取图像的尺寸并在同一时间或之后将图像流式传输到对象。
像这样的代码。
string page = pages[pageComboBox.SelectedIndex].pageImage; // image url
var stream = web.OpenRead(page);
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream; // First read stream
stream.Position = 0; // tried
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
pageIMG.Source = bitmap;
Image image = Image.FromStream(stream); // Second read stream
MessageBox.Show("Image Height = " + image.Height + " Image Width = " + image.Width);
我确实使用了位置的东西,但它给了我一个错误 -
'This stream does not support seek operations.'
请帮忙...
将源流复制到 MemoryStream:
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
...
memoryStream.Position = 0;
image = Image.FromStream(memoryStream);
...
}
但是,将流再次读入 WinForms Image 似乎毫无意义。您可以只查询 BitmapImage 的 PixelWidth 和 PixelHeight 属性:
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
...
MessageBox.Show(
"Image Height = " + bitmap.PixelHeight +
" Image Width = " + bitmap.PixelWidth);
}
我想获取图像的尺寸并在同一时间或之后将图像流式传输到对象。 像这样的代码。
string page = pages[pageComboBox.SelectedIndex].pageImage; // image url
var stream = web.OpenRead(page);
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream; // First read stream
stream.Position = 0; // tried
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
pageIMG.Source = bitmap;
Image image = Image.FromStream(stream); // Second read stream
MessageBox.Show("Image Height = " + image.Height + " Image Width = " + image.Width);
我确实使用了位置的东西,但它给了我一个错误 -
'This stream does not support seek operations.'
请帮忙...
将源流复制到 MemoryStream:
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
...
memoryStream.Position = 0;
image = Image.FromStream(memoryStream);
...
}
但是,将流再次读入 WinForms Image 似乎毫无意义。您可以只查询 BitmapImage 的 PixelWidth 和 PixelHeight 属性:
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
...
MessageBox.Show(
"Image Height = " + bitmap.PixelHeight +
" Image Width = " + bitmap.PixelWidth);
}