如何将位图对象作为图像保存到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函数只能接受一个字符串并将其保存为文件正文.
我怎样才能以这样的方式编写它,它允许我传入一个位图对象并将其保存为图像?也许作为一个流?还是作为字节数组?
我感谢任何帮助.
解决方法
您将使用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); } }