普通网友 2010-05-01 16:34
浏览 48
已采纳

c#视频相当于image.fromstream? 或者更改以下脚本的范围以允许视频

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;
            }
        }
    }
  • 写回答

2条回答 默认 最新

  • duanmianhong4893 2010-05-01 16:53
    关注

    Well I am not quite sure if you are wanting to simply save the file to the server or what. I'm not clear on the goal. However, if you are simply wanting to save the file to the server, it appears you have already done that with "file.SaveAs(path);". If that is the goal, just rip out the other stuff and your set. If you need more fine grain control of the file you can use the below code, using a different constructor as needed.

    byte[] buffer;
    int bytesRead = 0;
    System.IO.FileStream fs = new System.IO.FileStream("myfile.whatever"); // not sure about your constructor, look this one up
    while((bytesRead = file.InputStream.Read(buffer, 0, 1024)) > 0)
      fs.write(buffer, 0, bytesRead);
    fs.Close();
    

    Now I did not actually try this, but it should give you a good base from which you can start.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?