c# video equivalent to image.fromstream? Or changing the scope of the following script to allow vide
Posted
by Daniel
on Stack Overflow
See other posts from Stack Overflow
or by Daniel
Published on 2010-05-01T16:34:22Z
Indexed on
2010/05/01
16:37 UTC
Read the original article
Hit count: 211
The following is a part of an upload class in a c# script. I'm a php programmer, I've never messed with c# much but I'm trying to learn. This upload script will not handle anything except images, I need to adapt this class to handle other types of media also, or rewrite it all together. If I'm correct, I realize that
using (Image image = Image.FromStream(file.InputStream))
basically says that the scope of the following is Image, only an image can be used or the object is discarded? And also that the variable image is being created from an Image from the file stream, which I understand to be, like... the $_FILES array in php?
I dunno, I don't really care about making thumbnails right now either way, so if this can be taken out and still process the upload I'm totally cool with that, I just haven't had any luck getting this thing to take anything but images, even when commenting out that whole part of the class...
protected void Page_Load(object sender, EventArgs e)
{
string dir = Path.Combine(Request.PhysicalApplicationPath, "files");
if (Request.Files.Count == 0)
{
// No files were posted
Response.StatusCode = 500;
}
else
{
try
{
// Only one file at a time is posted
HttpPostedFile file = Request.Files[0];
// Size limit 100MB
if (file.ContentLength > 102400000)
{
// File too large
Response.StatusCode = 500;
}
else
{
string id = Request.QueryString["userId"];
string[] folders = userDir(id);
foreach (string folder in folders)
{
dir = Path.Combine(dir, folder);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
}
string path = Path.Combine(dir, String.Concat(Request.QueryString["batchId"], "_", file.FileName));
file.SaveAs(path);
// Create thumbnail
int dot = path.LastIndexOf('.');
string thumbpath = String.Concat(path.Substring(0, dot), "_thumb", path.Substring(dot));
using (Image image = Image.FromStream(file.InputStream))
{
// Find the ratio that will create maximum height or width of 100px.
double ratio = Math.Max(image.Width / 100.0, image.Height / 100.0);
using (Image thumb = new Bitmap(image, new Size((int)Math.Round(image.Width / ratio), (int)Math.Round(image.Height / ratio))))
{
using (Graphics graphic = Graphics.FromImage(thumb))
{
// Make sure thumbnail is not crappy
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.High;
graphic.CompositingQuality = CompositingQuality.HighQuality;
// JPEG
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1];
// 90% quality
EncoderParameters encode = new EncoderParameters(1);
encode.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
// Resize
graphic.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
// Save
thumb.Save(thumbpath, codec, encode);
}
}
}
// Success
Response.StatusCode = 200;
}
}
catch
{
// Something went wrong
Response.StatusCode = 500;
}
}
}
© Stack Overflow or respective owner