普通网友 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条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳