gpt4 book ai didi

c# - fileStream.CopyTo(memoryStream)后,memoryStream为null

转载 作者:可可西里 更新时间:2023-11-01 07:45:12 26 4
gpt4 key购买 nike

所以,我有获取 BitmapImage 的函数,我需要将它保存到 iso 存储并转换为 Base64(用于发送到服务器)。但是,从 fileStream 复制到 memoryStream 不成功。

public void SetImage(BitmapImage bitmap)
{
if (isoFiles.FileExists(Settings.FILE_AVATAR_JPG))
isoFiles.DeleteFile(Settings.FILE_AVATAR_JPG);

var fileStream = isoFiles.CreateFile(Settings.FILE_AVATAR_JPG);
var wb = new WriteableBitmap(bitmap);
wb.SaveJpeg(fileStream, 120, 120, 0, 85); // file is saved

var memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream); // here, memoryStream is null
byte[] result = memoryStream.ToArray();
fileStream.Close();

var base64 = Convert.ToBase64String(result);
}

最佳答案

Stream.CopyTofileStream 的当前位置复制,该位置已被 SaveJpeg() 更改,因此您需要重置它;

var memoryStream = new MemoryStream();

fileStream.Position = 0;
fileStream.CopyTo(memoryStream);

关于c# - fileStream.CopyTo(memoryStream)后,memoryStream为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com