4
我從資源獲取圖像(實際上是位圖類的實例),並且(將其寫入rtf流) - 我需要原始數據。從圖像實例獲取原始數據
此外,Image.Save方法不能使用,因爲它似乎關閉流保存到,我需要追加到RTF文件。我不想創建新的流,將圖像保存到它,然後將其複製到RTF流,因爲性能問題。
我從資源獲取圖像(實際上是位圖類的實例),並且(將其寫入rtf流) - 我需要原始數據。從圖像實例獲取原始數據
此外,Image.Save方法不能使用,因爲它似乎關閉流保存到,我需要追加到RTF文件。我不想創建新的流,將圖像保存到它,然後將其複製到RTF流,因爲性能問題。
創建通過您要附加到流通過一個過濾器流,但不會關閉該流時,它本身是封閉的:
public class AppendingStream : Stream
{
private readonly long _startPos;
private readonly Stream _sink;
public AppendingStream(Stream sink)
{
if(sink == null)
throw new ArgumentNullException();
if(!sink.CanWrite)
throw new ArgumentException();
_sink = sink;
try
{
_startPos = sink.Position;
}
catch(NotSupportedException)
{
_startPos = -1;
}
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return _sink.CanSeek && _startPos != -1;
}
}
public override bool CanTimeout
{
get
{
return _sink.CanTimeout;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override long Length
{
get
{
if(_startPos == -1)
throw new NotSupportedException();
return _sink.Length - _startPos;
}
}
public override long Position
{
get
{
return _sink.Position - _startPos;
}
set
{
_sink.Position = value + _startPos;
}
}
public override void Flush()
{
_sink.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override int ReadByte()
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
if(origin == SeekOrigin.Begin)
return _sink.Seek(offset + _startPos, SeekOrigin.Begin) - _startPos;
else
return _sink.Seek(offset, origin);
}
public override void SetLength(long value)
{
if(_startPos == -1)
throw new NotSupportedException();
_sink.SetLength(value + _startPos);
}
public override void Write(byte[] buffer, int offset, int count)
{
_sink.Write(buffer, offset, count);
}
public override void WriteByte(byte value)
{
_sink.WriteByte(value);
}
}
那麼你已經不再有問題該流被關閉,因爲它並不重要。
處理尋找新位置的位是因爲某些圖像格式是以非順序方式寫入的。如果底層的流不支持它,它將不起作用,但是那樣不會起作用。