我在使用内存流进行序列化时遇到问题.这是我的代码:
/// <summary>
/// serializes the given object into memory stream
/// </summary>
/// <param name="objectType">the object to be serialized</param>
/// <returns>The serialized object as memory stream</returns>
public static MemoryStream SerializeToStream(object objectType)
{
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, objectType);
return stream;
}
/// <summary>
/// deserializes as an object
/// </summary>
/// <param name="stream">the stream to deserialize</param>
/// <returns>the deserialized object</returns>
public static object DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin); …Run Code Online (Sandbox Code Playgroud) 我使用的是.NET 4.5,如果我尝试使用"CreateFromDirectory"压缩整个目录,ZipFile类的效果很好.但是,我只想在目录中压缩一个文件.我试着指向一个特定的文件(文件夹\ data.txt),但这不起作用.我考虑过ZipArchive类,因为它有一个"CreateEntryFromFile"方法,但似乎这只允许你创建一个现有文件的条目.
有没有办法简单地压缩一个文件而不创建一个空的zipfile(有问题),然后使用ZipArchiveExtension的"CreateEntryFromFile"方法?
**这也是假设我正在开发一个目前无法使用第三方附加组件的公司计划.
示例来自:http://msdn.microsoft.com/en-us/library/ms404280%28v=vs.110%29.aspx
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
Run Code Online (Sandbox Code Playgroud)
但是如果是startPath @"c:\example\start\myFile.txt;",则会抛出目录无效的错误.
我使用以下方法将图像加载到Picture Box中:
picturebox1.Image = Image.FromFile()
Run Code Online (Sandbox Code Playgroud)
我用以下方法保存它:
Bitmap bm = new Bitmap(pictureBox1.Image);
bm.Save(FileName, ImageFormat.Bmp);
Run Code Online (Sandbox Code Playgroud)
它在创建新文件时工作得很好,但是当我尝试替换现有图像时,我会抛出以下运行时错误:
GDI +中发生了一般错误
那么我该怎么做才能解决这个问题呢?
好的,我在这里和这里都遇到过一些文章,但他们并没有做我需要做的事情,而且我遇到了一些麻烦.
我收到一段加密的数据作为内存流.我需要以某种方式将内存流写入文件(我编写模型的方式,因为字符串最好),然后从文件中检索字符串并将其作为内存流发送到要解密的服务.我只是使用流读取器将内存流存储为字符串,并使用编码将字符串读入内存.
问题是我收到一个错误,说我的加密数据已损坏.我认为这意味着我以某种方式改变了字节.
以下是将memorystream读入字符串的代码:
using (StreamReader reader = new StreamReader(dataKeyResponse.CiphertextBlob))
{
encryptedDataKey = reader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
以下是将从文件中检索到的字符串读取到内存流中的代码:
MemoryStream mStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(encryptedKey));
Run Code Online (Sandbox Code Playgroud)
我认为这样做的步骤是ASCIIEncoding,但后来我实现了上面的解决方法,将字节数组转换为内存流,并得到了同样的错误.
byte[] bytes = new byte[encryptedKey.Length*sizeof (char)];
System.Buffer.BlockCopy(encryptedKey.ToCharArray(), 0, bytes, 0, bytes.Length);
string decryptedKey;
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(bytes, 0, bytes.Length);
var decryptRequest = new DecryptRequest()
{CiphertextBlob = mStream};
var decryptResponse = client.Decrypt(decryptRequest);
using (StreamReader reader = new StreamReader(decryptResponse.Plaintext))
{
decryptedKey = reader.ReadToEnd();
}
}
Run Code Online (Sandbox Code Playgroud)
我假设(1)有些东西以某种方式改变数据而不是其他一些错误; (2)它在流 - >字符串或字符串 - …
我知道如何保存Streams,但我想获取该流并创建缩略图和其他大小的图像,但我不知道如何将byte []保存到Azure Blob存储.
这就是我现在要做的就是保存Stream:
// Retrieve reference to a blob named "myblob".
CloudBlockBlob _blockBlob = container.GetBlockBlobReference("SampleImage.jpg");
// upload from Stream object during file upload
blockBlob.UploadFromStream(stream);
// But what about pushing a byte[] array? I want to thumbnail and do some image manipulation
Run Code Online (Sandbox Code Playgroud) 我有一个格式良好的XML,没有任何空格.它必须是那样的.
当我将它加载到XMLDocument进行签名时,自闭标签会获得额外的空白区域
<cEAN/>
Run Code Online (Sandbox Code Playgroud)
变为:
<cEAN />
Run Code Online (Sandbox Code Playgroud)
一旦签署此文档,就无法删除空白区域.
PreserveWhiteSpace属性对结果没有任何影响.
我该如何改变这种行为?
我已经搜索了一段时间,虽然它应该很简单,但我就是无法让它工作。根据我见过的例子,这是我到目前为止得到的:
SomeAppService.cs
public async Task<FileStream> Download(long? id)
{
var attachment = await _repository.FirstOrDefaultAsync(x => x.Id == id);
var fileStream = new FileStream($"{attachment.FileName}.{attachment.FileExtension}",
FileMode.Create, FileAccess.Write);
fileStream.Write(attachment.File, 0, attachment.File.Length);
return fileStream;
}
Run Code Online (Sandbox Code Playgroud)
可以注意到,“FileName”、“FileExtension”和“File”(即前述的字节数组)存储在数据库中。附件可以是任何类型的文件,但上传方法中禁止的扩展名除外(未显示)。然后在我的控制器中我有:
SomeController.cs
[AllowAnonymous]
[HttpGet("Download/{id}")]
public async Task<IActionResult> Download(long? id)
{
var fileStream = await appService.Download(id);
return new FileStreamResult(fileStream, "application/octet-stream");
}
Run Code Online (Sandbox Code Playgroud)
然而,当我到达下载端点时,我最终得到一个名为“response”的文件,没有扩展名,大小为 0 字节。
资源:
将 MemoryStream 保存到文件或从文件加载(255 个赞成票的响应让我了解了如何将字节数组转换为文件流,但我不知道这是否有效)
我正在使用C#和SQLServer 2012开发数据库项目.在我的一个表单中,我有一个PDF文件,其中包含一些存储在表中的其他信息.这是成功的,但当我想检索存储的信息时,我有一个显示PDF文件的问题,因为我无法显示它,我不知道如何显示它.
我读过一些文章说它无法用内存流中的Adobe PDF查看器显示,有什么办法吗?
这是我从数据库中检索数据的代码:
sql_com.CommandText = "select * from incoming_boks_tbl where [incoming_bok_id]=@incoming_id and [incoming_date]=@incoming_date";
sql_com.Parameters.AddWithValue("incoming_id",up_inco_num_txt.Text);
sql_com.Parameters.AddWithValue("incoming_date", up_inco_date_txt.Text);
sql_dr = sql_com.ExecuteReader();
if(sql_dr.HasRows)
{
while(sql_dr.Read())
{
up_incoming_id_txt.Text = sql_dr[0].ToString();
up_inco_num_txt.Text = sql_dr[1].ToString();
up_inco_date_txt.Text = sql_dr[2].ToString();
up_inco_reg_txt.Text = sql_dr[3].ToString();
up_inco_place_txt.Text = sql_dr[4].ToString();
up_in_out_txt.Text = sql_dr[5].ToString();
up_subj_txt.Text = sql_dr[6].ToString();
up_note_txt.Text = sql_dr[7].ToString();
string file_ext = sql_dr[8].ToString();//pdf file extension
byte[] inco_file = (byte[])(sql_dr[9]);//the pdf file
MemoryStream ms = new MemoryStream(inco_file);
//here I don't know what to do with memory stream file data and where …Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像保存在Android设备的文件夹中。我正在使用的代码如下
var newFolder = AndroidEnvironment.GetExternalStoragePublicDirectory(AndroidEnvironment.DirectoryPictures).AbsolutePath + "/NewFolder";
Directory.CreateDirectory(cameraFolder);
byte[] reducedImage = ResizeImageAndroid(imageData, 50, 50, 70);
Image image = new Image {Source = ImageSource.FromStream(() => new MemoryStream(reducedImage))};
Run Code Online (Sandbox Code Playgroud)
我想在“ newFolder ” 中将缩小的图像另存为jpg文件。我不确定方向是否正确,如果将reducedImage或image保存为newFolder中的jpg文件,那会很好。我正在使用Xamarin,此代码在Android项目中。
我已经检查了这篇文章,我不明白那里发生了什么。
的背景:
我有以下WriteFileToStream函数,用于完成一个简单的工作:从文件中获取数据并将其复制到Stream.
我最初使用的是Stream.CopyTo(Stream)方法.但是,经过漫长的调试过程后,我发现这是导致我的处理管道中出现"损坏数据"错误的原因.
概要:
使用Stream.CopyTo(Stream)方法产生65536个字节的数据,并且流不能正确处理.
使用Stream.Write(...)方法可以生成45450个字节的数据,并且流正确处理.
问题:
任何人都可以看到为什么以下使用CopyTo可能导致无关数据被写入流?
请注意:WriteFileToStream中的最终代码取自对此问题的回答:将MemoryStream保存并加载到文件中
public static void WriteFileToStream(string fileName, Stream outputStream)
{
FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);
long fileLength = file.Length;
byte[] bytes = new byte[fileLength];
file.Read(bytes, 0, (int)fileLength);
outputStream.Write(bytes, 0, (int)fileLength);
file.Close();
outputStream.Close();
// This was corrupting the data - adding superflous bytes to the result...somehow.
//using (FileStream file = File.OpenRead(fileName))
//{
// //
// file.CopyTo(outputStream);
//}
}
Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×2
filestream ×2
memorystream ×2
android ×1
arrays ×1
asp.net-core ×1
azure ×1
bitmap ×1
c#-4.0 ×1
encryption ×1
gdi+ ×1
pdf ×1
signedxml ×1
string ×1
xamarin ×1
xmldocument ×1
zip ×1
zipfile ×1