对于.Net MemoryStream对象实例,使用后是否需要显式关闭它?或者不需要关闭它?哪种方法最好?
我使用的是VSTS2008 + .Net 3.5 + C#.
在Delphi XE中,我从剪贴板中捕获CF_UNICODETEXT数据.结果是一个以两个空字节终止的流.要获取复制到剪贴板的实际字符串,我需要删除空值.
这个类似的问题包含一个从TMemoryStream转换为Delphi的unicode字符串的好方法:
function MemoryStreamToString(M: TMemoryStream): string;
begin
SetString(Result, M.Memory, M.Size div SizeOf(Char));
end;
Run Code Online (Sandbox Code Playgroud)
但是,在我的情况下,这将产生一个包含尾随空值的字符串.我可以通过限制大小来解决这个问题:
function ClipboardMemoryStreamToString(M: TMemoryStream): string;
begin
SetString(Result, M.Memory, (M.Size - SizeOf(Char)) div SizeOf(Char));
end;
Run Code Online (Sandbox Code Playgroud)
......但这感觉很难看,"特殊情况".我想知道是否有更简洁的方法对此进行编码,以便后来查看代码的任何人(我!)都不会立即询问"为什么会从流中删除尾随的字符?"
编辑:预先解决问题的一种方法是添加评论.但是,除此之外?
这是示例代码...但我得到了serializationException.
xlbook是对象,我想将此对象保存到memorystream.
unsafe public void Save(IStream stream, bool clearDirty, Excel.Workbook xlbook)
{
try
{
//if (stream == null)
//{
// return;
//}
//object data = xlbook;
if (xlbook == null)
{
return;
}
// convert data to byteArray
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
//AppDomain currentDomain = AppDomain.CurrentDomain;
//currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
//here im getting exception.
binaryFormatter.Serialize(memoryStream, xlbook);
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
//get memory pointer
int cb;
int* pcb = &cb;
//save data
byte[] arrayLen …Run Code Online (Sandbox Code Playgroud) 是否有任何理由为什么这段代码不应该生成带有Slappy一词的内存流?
private MemoryStream StringBuilderToMemoryStream(StringBuilder source)
{
MemoryStream memoryStream = new MemoryStream();
StreamWriter streamWriter = new StreamWriter(memoryStream);
streamWriter.Write("slappy");
return memoryStream;
}
Run Code Online (Sandbox Code Playgroud)
即使我说它streamWriter.Write(source.toString());失败了.
有趣的是,它适用于调用此例程的方法之一,但不适用于任何其他方法.
我称之为的顺序也没有区别.
但无论如何,即使我调用上述内容,从有效的方法来看,输出仍然是一个空的MemoryStream.
有什么想法吗?
当我将一些流写入新的内存流时,内存流的长度和容量最初都设置为0,并随着写入过程一起增长.但在某些时候,容量增长的速度要快于长度.
写操作完成后,我得到正确的长度(与原始输入流的长度相同),但容量更大.
这是对的吗?
它们不应该是一样的吗?
我找不到任何关于它的信息
我需要截取屏幕的一部分并检查屏幕截图是否与pictureBox2.
这是行不通的:
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
Run Code Online (Sandbox Code Playgroud)
错误是
值不能为空。
参数名称:编码器

我的代码如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height,
PixelFormat.Format32bppArgb);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width,
SystemInformation.VirtualScreen.Height,
PixelFormat.Format32bppArgb);
Graphics screenGraph = Graphics.FromImage(screenshot);
screenGraph.CopyFromScreen(
SystemInformation.VirtualScreen.X …Run Code Online (Sandbox Code Playgroud) 我使用这种方法合并现有的 pdf 文档并向其中添加新的 pdf 文档(在内存中不是物理的)问题我有 2 个内存流,一个用于合并的 pdf,一个用于新文档,然后将其转换为字节数组,我想连接这些拖车阵列
public static byte[] merge(List<String> pdf)
{
MemoryStream copystream;
MemoryStream ms;
using (ms = new MemoryStream())
{
Document document;
using (document = new Document())
{
using (PdfWriter wri = PdfWriter.GetInstance(document, ms))
{
wri.CloseStream = false;
document.Open();
document.SetPageSize(iTextSharp.text.PageSize.A4); // for vertical layout
document.Add(new Paragraph("Hello"));
document.Close();
copystream = new MemoryStream();
Document doc = new Document();
using (PdfCopy copy = new PdfCopy(doc, copystream))
{
copy.CloseStream = false;
copy.Open();
doc.Open();
// copy.AddPage(PageSize.A4, 0);
for (int i = …Run Code Online (Sandbox Code Playgroud) 我编写了如下方法来将多个 Memorystream 绑定到 ziparchive。该代码适用于一个流,但如果我通过迭代添加多个流,则它会在 for 循环的第二行中显示以下错误。
System.IO.IOException: 'Entries cannot be created
while previously created entries are still open.'
Run Code Online (Sandbox Code Playgroud)
我的代码,
using (var zip = new ZipArchive(outputStream, ZipArchiveMode.Create,
leaveOpen: false))
{
for (int i = 0; i < msList.Count; i++)
{
msList[i].Position = 0;
var createenter = zip.CreateEntry("123"+i+".jpg",
CompressionLevel.Optimal);
msList[i].CopyTo(createenter.Open());
}
}
Run Code Online (Sandbox Code Playgroud) 我需要在 ASP.NET 控制器方法中将文档作为 MemoryStream 返回以在网页上下载它。我不想把这个文档保存在文件上,然后把它读成 MemoryStream 并返回。注意:重载方法 WordprocessingDocument.CreateFromTemplate(template) 没有流选项 vs WordprocessingDocument.Create(stream, ...)
保存临时文件的解决方案如下。
public static MemoryStream GetWordDocumentFromTemplate()
{
string tempFileName = Path.GetTempFileName();
var templatePath = AppDomain.CurrentDomain.BaseDirectory + @"Controllers\" + templateFileName;
using (var document = WordprocessingDocument.CreateFromTemplate(templatePath))
{
var body = document.MainDocumentPart.Document.Body;
//add some text
Paragraph paraHeader = body.AppendChild(new Paragraph());
Run run = paraHeader.AppendChild(new Run());
run.AppendChild(new Text("This is body text"));
OpenXmlPackage savedDoc = document.SaveAs(tempFileName); // Save result document, not modifying the template
savedDoc.Close(); // can't read if it's open
document.Close();
}
var …Run Code Online (Sandbox Code Playgroud) 我正在编写一些库代码,其中有一个byte[]在内存中保存一些数据。我想将byte[]viaStream对象的某些部分公开给库使用者。例如,我希望暴露的Stream能够访问从位置 50 到byte[]. 公开的Stream将是可读和可查找的,但不可写。
有没有一种简单的方法可以做到这一点(希望不需要编写我自己的Stream实现)而无需在内存中创建数据副本?我试图使用新Memory<T>的API,但由于没有得到很远MemoryStream的构造函数不能带一个Memory<T>参数,它似乎是我不能让一个byte[]从Memory<byte>没有做一个副本:
byte[] byteArray = new byte[100];
Memory<byte> memory = byteArray;
// Let's say I want to expose the second half of the byte[] via a Stream
var slicedMemory = memory.Slice(50);
// The only way to construct a MemoryStream from Memory<byte> is to call ToArray and get a byte[]
// But this makes …Run Code Online (Sandbox Code Playgroud) memorystream ×10
c# ×9
.net ×2
.net-core ×1
arrays ×1
asp.net ×1
assemblies ×1
capacity ×1
copy ×1
delphi ×1
ms-word ×1
openxml ×1
pdf ×1
save-image ×1
stream ×1
ziparchive ×1