如何将位图对象作为图像保存到Amazon S3?
我已经完成了所有设置但是我有限的C sharp阻止了我完成这项工作。
// I have a bitmap iamge
Bitmap image = new Bitmap(width, height);
// Rather than this
image.save(file_path);
// I'd like to use S3
S3 test = new S3();
test.WritingAnObject("images", "testing2.png", image);
// Here is the relevant part of write to S3 function
PutObjectRequest titledRequest = new PutObjectRequest();
titledRequest.WithMetaData("title", "the title")
.WithContentBody("this object has a title")
.WithBucketName(bucketName)
.WithKey(keyName);
正如您所看到的,S3函数只能接受一个字符串并将其保存为文件正文。
如何以这样的方式编写它,它允许我传入一个位图对象并将其保存为图像?也许作为一个流?或者作为字节数组?
我感谢任何帮助。
答案 0 :(得分:13)
您可以使用WithInputStream
或WithFilePath
。例如,在将新图像保存到S3时:
using (var memoryStream = new MemoryStream())
{
using(var yourBitmap = new Bitmap())
{
//Do whatever with bitmap here.
yourBitmap.Save(memoryStream, ImageFormat.Jpeg); //Save it as a JPEG to memory stream. Change the ImageFormat if you want to save it as something else, such as PNG.
PutObjectRequest titledRequest = new PutObjectRequest();
titledRequest.WithMetaData("title", "the title")
.WithInputStream(memoryStream) //Add file here.
.WithBucketName(bucketName)
.WithKey(keyName);
}
}
答案 1 :(得分:2)
设置请求对象的InputStream属性:
titledRequest.InputStream = image;