ASP.NET uploading a file to Amazon S3 - c#

I am in the process of uploading images to Amazon S3, however i keep getting the error "Please specify either a Filename, provide a FileStream or provide a ContentBody to PUT an object into S3."
Basically i am uploading an image from a fileupload control and then hitting the code below. It uploads locally fine, but not to Amazon. The Credentials are alright so it only errors when it comes to uplaoding.
Can anyone see why this is happening please?
protected void uploadImg(int prodId, int prodFormat)
{
if (imgPack.HasFile)
{
string fileExt = Path.GetExtension(imgPack.PostedFile.FileName);
string filename = "img" + prodId + ".jpg";
// Specify the upload directory
string directory = Server.MapPath(#"\images\packshots\");
if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".png")
{
if (packUK.PostedFile.ContentLength < 716800)
{
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(packUK.FileContent);
// Calculate the new image dimensions
decimal origWidth = originalBMP.Width;
decimal origHeight = originalBMP.Height;
decimal sngRatio = origHeight / origWidth;
int newHeight = 354; //hight in pixels
decimal newWidth_temp = newHeight / sngRatio;
int newWidth = Convert.ToInt16(newWidth_temp);
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
string accessKey = "KEY HERE";
string secretKey = "KEY HERE";
AmazonS3 client;
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
PutObjectRequest request = new PutObjectRequest();
request.BucketName="MyBucket";
request.CannedACL = S3CannedACL.PublicRead;
request.Key = "images/" + filename;
S3Response response = client.PutObject(request);
}
//newBMP.Save(directory + filename);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
}
}
else
{
notifybar.Attributes.Add("style", "display:block;");
notifybar.Attributes.Add("class", "failed");
notifyText.Text = "Error Text Here";
}
}
else
{
notifybar.Attributes.Add("style", "display:block;");
notifybar.Attributes.Add("class", "failed");
notifyText.Text = "Error Text Here";
}
}

You need to assign File or InputStream property of PutObjectRequest object. The code fragment should look like this one:
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
{
var stream = new System.IO.MemoryStream();
originalBMP.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
PutObjectRequest request = new PutObjectRequest();
request.InputStream = stream;
request.BucketName="MyBucket";
request.CannedACL = S3CannedACL.PublicRead;
request.Key = "images/" + filename;
S3Response response = client.PutObject(request);
}

Related

C# How to reduce the size of the stream (image)

All I have is the Stream of the file (the file is image/jpeg type). I want to reduce the quality of the image (example: original 2MB -> 200KB).
var contentStream = ImageSource.FromStream(() => { return _MediaFile.GetStream(); });
newImg.Source = contentStream;
_ReportModel.Add(new ReportPicture()
{
Id = layout.Id,
Path = _MediaFile.Path,
Content = _MediaFile.GetStream()
});
Before I upload the _ReportModel to my server using REST API, I want to reduce the size of the Content which contains the stream of the read image (type jpeg).
I need something like:
private Stream ReduceStreamSize(Stream oldStream)
{
var reducedStream = //doSomething(oldStream);
return reducedStream;
}
How can I do this?
Update
I have tried this:
public Stream CompressImage(string imagePath)
{
try
{
// BitmapFactory options to downsize the image
BitmapFactory.Options o = new BitmapFactory.Options();
o.InJustDecodeBounds = true;
o.InSampleSize = 6;
// factor of downsizing the image
FileStream inputStream = new FileStream(imagePath, FileMode.Open);
//Bitmap selectedBitmap = null;
BitmapFactory.DecodeStream(inputStream, null, o);
inputStream.Close();
// The new size we want to scale to
int REQUIRED_SIZE = 75;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.OutWidth / scale / 2 >= REQUIRED_SIZE &&
o.OutHeight / scale / 2 >= REQUIRED_SIZE)
{
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.InSampleSize = scale;
inputStream = new FileStream(imagePath, FileMode.Open);
Bitmap selectedBitmap = BitmapFactory.DecodeStream(inputStream, null, o2);
inputStream.Close();
FileStream outputStream = new FileStream(imagePath, FileMode.Open);
selectedBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outputStream);
return outputStream;
}
catch (Exception e)
{
return null;
}
}
But the image is not showing. There must be an error while compressing it.

Get Size image from link

I want to check size image from url in C#.
Ex: Url: http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg
Download and check:
string image = #"http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg";
byte[] imageData = new WebClient().DownloadData(image);
MemoryStream imgStream = new MemoryStream(imageData);
Image img = Image.FromStream(imgStream);
int wSize = img.Width;
int hSize = img.Height;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(#"http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg";);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
Image img = Image.FromStream(stream);
stream.Close();
MessageBox.Show("Height: " + img.Height + " Width: " + img.Width);
For optimization, I suggest to use #Backs answer with async/await to download the image:
public async Task<Size> GetImageSizeFromUrl(string url)
{
var imageData = await new System.Net.WebClient().DownloadDataTaskAsync(url);
var imgStream = new MemoryStream(imageData);
var img = System.Drawing.Image.FromStream(imgStream);
return new Size(img.Width, img.Height);
}

The process cannot access the file because it is being used by another process asp.net [duplicate]

This question already has answers here:
"The process cannot access the file because it is being used by another process" with Images
(7 answers)
Closed 7 years ago.
I have the following method which resizes a given image and caches it
[OutputCache(Duration = 3600, VaryByParam = "id")]
public ActionResult UserFace(int id)
{
var cacheKey = String.Format("userPhotoURICacheKey_{0}", id);
var defaultFileCacheKey = String.Format("userPhotoDefaultCacheKey");
HttpContext.Cache[cacheKey] = HttpContext.Cache[cacheKey] ?? UserService.GetPhotoURI(id) ?? string.Empty;
var userPhoto = (String)HttpContext.Cache[cacheKey];
if (!string.IsNullOrEmpty(userPhoto))
{
var filename = id + ".jpg";
//crop and resize
var image = Image.FromFile(new Uri(userPhoto).LocalPath);
const int resW = 200;
const int resH = 200;
var minEdge = Math.Min(image.Width, image.Height);
var scaleFactor =(float) minEdge/Math.Min(resH, resW);
var newImage = new Bitmap(resW, resH);
var sufrace = Graphics.FromImage(newImage);
sufrace.InterpolationMode = InterpolationMode.HighQualityBicubic;
sufrace.DrawImage(image, 0, 0, image.Width / scaleFactor, image.Height / scaleFactor);
var stream = new MemoryStream();
newImage.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;
var file = File(stream, "image/jpg", filename);
return file;
}
HttpContext.Cache[defaultFileCacheKey] = HttpContext.Cache[defaultFileCacheKey] ?? File("~/Content/images/no_photo.jpg", "image/jpg", "nophoto.jpg");
return (FileResult)HttpContext.Cache[defaultFileCacheKey];
}
In another method I'm trying to update a cached image with a new one
public void UploadUserImage(byte[] arr, int userId)
{
Stream stream = new MemoryStream();
stream.Write(arr, 0, arr.Length);
using (var dataManager = factory.GetDataManager())
{
var userRepository = dataManager.GetRepository<IUserRepository>();
var user = GetByID(userId);
user.PhotoURI =
storageFactory.CreateFileStorageSender()
.Send(storageConfig.UsersPhotoPath, user.Id.ToString(), stream);
user.PhotoSize = stream.Length;
userRepository.Save(user);
dataManager.Commit();
}
}
And I'm getting "The process cannot access the file because it is being used by another process" error.
Dispose image after you're done with resize to use it again.
You need to dispose the "file", "image" and "newImage" objects from your code. I think the handles to the file object is giving you this error.

Error at sending a picture from server to client

I have 2 Apps (server - client ) .
The server is modified version of TVsharp (Application that stream local analog tv signal using RTLSDR)
Each frame of the streamed video is a grayscale array of bytes .
I have modified it so it re sizes and sends each frame for a client over TCP socket
The client is supposed to receive the frames through the socket as Image objects and displays them in a picture box
Im getting invalid parameters error .
After i added a delay (Thread.Sleep()) it started to display one frame and then it gives invalid parameter exception (after the sleeping time)
This is the part of TVsharp that dose the sending :
Grayscale is an array that contains the brightness for each pixel
private string drive = "E:\\";
private string file = "0";
private string extension = ".bmp";
private string path2 = "E:\\test\\";
private string fullpath;
private int file_counter = 0;
Bitmap bitmap2 = new Bitmap(_pictureWidth, _pictureHeight);
var data2 = bitmap2.LockBits(new Rectangle(Point.Empty, bitmap2.Size),
ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);
Marshal.Copy(GrayScaleValues, 0, data2.Scan0, GrayScaleValues.Length);
bitmap2.UnlockBits(data2);
bitmap2.Save(fullpath, System.Drawing.Imaging.ImageFormat.Bmp);
string Npath = path2 + file + extension;
Image img = Image.FromFile(fullpath);
// Size size = new Size(982, 543);
// ResizeImage(img, size);
Rectangle cropRect = new Rectangle(80, 0, 240, 175);
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
}
//double scale = 203 / 96;
int width = (int)(target.Width);
int height = (int)(target.Height);
BinaryWriter brn = new BinaryWriter(s);
System.Drawing.Bitmap bmpScaled = new System.Drawing.Bitmap(target, width, height);
bmpScaled.Save(Npath);
byte[] imageArray = File.ReadAllBytes(Npath);
sw.WriteLine(imageArray.Length.ToString());
sw.Flush();
// Thread.Sleep(500);
brn.Write(imageArray);
//Thread.Sleep(500);
_detectLevel = Convert.ToInt32(_maxSignalLevel * _detectorLevelCoef);
_agcSignalLevel = agcMaxLevel;
}
This client the client segment that suppose to get the frames and display them
flag = true;
TcpClient client = new TcpClient(textBox1.Text, Convert.ToInt32(textBox2.Text));
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.Flush();
BinaryFormatter formatter = new BinaryFormatter();
string msg = sr.ReadLine();
MessageBox.Show("It's working !! the message is " + msg);
BinaryReader Brn = new BinaryReader(s);
Thread.Sleep(5000);
while (flag)
{
// Thread.Sleep(5000);
int size = Convert.ToInt32(sr.ReadLine());
label3.Text = "Size is " + size;
byte[] imagerray = Brn.ReadBytes(size);
MemoryStream ms = new MemoryStream(imagerray);
Thread.Sleep(10000);
Image image = Image.FromStream(ms);
ResizeImage(image, this.Size);
pictureBox1.Image = image;
Thread.Sleep(10);
}
}

How do you retrieve a snapshot of a website in ASP.NET?

Could anyone tell me how I could retrieve an image or a thumbnail of a website through my ASP.NET application? I have seen this functionality in a few sites such as Alexa etc.
Try SnapCasa's free and easy to use service. Just form your image tag like this:
<img src="http://SnapCasa.com/Get.aspx?code=[code]&size=[size]&url=[url]" />
Requires sign-up, but it's free for 500,000 requests a month. [code] is an api key that they provide after sign-up. [size] is one of three sizes available. [url] is the website address to the site for which you want to display a thumbnail.
If you want to work with the image from your code, here are a couple of helper methods:
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}
static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
Then, in your code, just use:
//get byte array for image
var imageBytes = GetBytesFromUrl("http://SnapCasa.com/Get.aspx?code=[code]&size=[size]&url=[url]");
//save file to disk
WriteBytesToFile("c:\someImageFile.jpg", imageBytes);
Should be doable using a web-browser object and saving the view port to a bitmap resized to thumbnail.
I have not tested this code but try tweaking it after substituting for the thumbnail params.
using (WebBrowser wb = new WebBrowser()) {
wb.ScrollBarsEnabled = false;
wb.AllowNavigation = true;
wb.ScriptErrorsSuppressed = true;
wb.ClientSize = new Size(thumbInfo_viewportWidth, thumbInfo_viewportHeight);
if ((thumbInfo_Uri != null)) {
wb.Navigate(thumbInfo_Uri.AbsoluteUri);
} else {
wb.Navigate("about:blank");
HtmlDocument doc = wb.Document.OpenNew(true);
doc.Write(thumbInfo_HTML);
wb.Refresh(WebBrowserRefreshOption.Completely);
}
// create an image of the client area of the webbrowser control, than
// scale it down to the dimensions specified.
if ((wb.Document != null && wb.Document.Body != null)) {
Rectangle rec = default(Rectangle);
rec.Size = wb.ClientSize;
using (Bitmap fullSizeBitmap = new Bitmap(thumbInfo_viewportWidth, thumbInfo_viewportHeight)) {
wb.DrawToBitmap(fullSizeBitmap, wb.Bounds);
using (Bitmap scaledBitmap = new Bitmap(thumbInfo_width, thumbInfo_height)) {
using (Graphics gr = Graphics.FromImage(scaledBitmap)) {
gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = Drawing2D.InterpolationMode.High;
Rectangle rect = new Rectangle(0, 0, thumbInfo_width, thumbInfo_height);
gr.DrawImage(fullSizeBitmap, rect, 0, 0, rec.Size.Width, rec.Size.Height, GraphicsUnit.Pixel);
scaledBitmap.Save(thumbInfo_physicalPath);
}
}
}
}
}
One thing to note that it is an expensive process.

Categories