如何从上传的图像文件中获取高度和宽度

时间:2022-07-06 07:46:44

I am using ASP.NET 2.0. I am trying to get the height & width of an image file I uploaded using the FileUpload control. Once uploaded, the image is kept in the db, not the file system. It seems I should be able to use something like the following code to do this but I can't get it to work.

我正在使用ASP.NET 2.0。我试图获取我使用FileUpload控件上传的图像文件的高度和宽度。上传后,映像将保留在db中,而不是文件系统中。似乎我应该能够使用类似下面的代码来做到这一点,但我无法让它工作。

Dim strm As Stream = oPostedFile.InputStream
dim i as image
i = System.Drawing.Image.FromStream(strm)

3 个解决方案

#1


I have found the solution.

我找到了解决方案。

Dim s As Stream = oPostedFile.InputStream
Dim i As Image = System.Drawing.Image.FromStream(s)

intFileWidth = i.PhysicalDimension.Width
intFileHeight = i.PhysicalDimension.Height

#2


Get the Height and width of the image type file by the following code

通过以下代码获取图像类型文件的高度和宽度

System.Drawing.Image imgFile = 
System.Drawing.Image.FromStream(fupDeviceImage.PostedFile.InputStream);
  if (imgFile.PhysicalDimension.Width > 500 || imgFile.PhysicalDimension.Height > 500)
  {
      cvDeviceImage.IsValid = false;
      fupDeviceImage.Focus();
      return;
  }

#3


In this Code you can see that uploaded image with their height and width and Convert it in to 400 to 800 pixel approximate*

在此代码中,您可以看到上传的图像的高度和宽度,并将其转换为400到800像素近似*

![First of all you need these server controls to Upload and display image ...that is show in this link ][1]

![首先,您需要这些服务器控件来上传和显示图像...在此链接中显示] [1]

[1]: http://i.stack.imgur.com/X0wNJ.png

You also need one panel server control to add Image in page dynamically;

您还需要一个面板服务器控件来动态地在页面中添加Image;

    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>

Then add in the Asp.cs class code for the Button1_click:

然后为Button1_click添加Asp.cs类代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = FileUpload1.FileName + "<br>" + FileUpload1.PostedFile.ContentType + "<br>" + FileUpload1.PostedFile.ContentLength;
        string s = Request.MapPath(Request.ApplicationPath + "/upload/" + FileUpload1.FileName);
        try
        {
            FileUpload1.SaveAs(s);
            Label1.Text += "<br>file upload success..";
            //For Get Uploaded Image height and Width :-
            System.Drawing.Image im = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
            double h = im.PhysicalDimension.Height;
            double w = im.PhysicalDimension.Width;
            Label2.Text = "Height:" + h + "...Widht: " + w;
            //For Display a Image in Panal control and set it height and width:-
            Image img = new Image();
            img.ImageUrl = "/upload/" + FileUpload1.FileName;
            if (h > w)
            {
                while (h > 800)
                {
                    h = h * 0.5;
                }
            }
            else
            {
                while (h > 400)
                {
                    h = h * 0.5;
                }
            }
            img.Height = new Unit(h);
            if (w > h)
            {
                while (w > 800)
                {
                    w = w * 0.5;
                }
            }
            else
            {
                while (w > 400)
                {
                    w = w * 0.5;
                }
            }
            img.Width = new Unit(w);
            Panel1.Controls.Add(img);
            Label2.Text += "<br>Now..Height:" + h + "...Widht: " + w;
        }
        catch
        {
            Label2.Text = "<br>choose your file..";
            Label1.Text = "";
        }
    } 

#1


I have found the solution.

我找到了解决方案。

Dim s As Stream = oPostedFile.InputStream
Dim i As Image = System.Drawing.Image.FromStream(s)

intFileWidth = i.PhysicalDimension.Width
intFileHeight = i.PhysicalDimension.Height

#2


Get the Height and width of the image type file by the following code

通过以下代码获取图像类型文件的高度和宽度

System.Drawing.Image imgFile = 
System.Drawing.Image.FromStream(fupDeviceImage.PostedFile.InputStream);
  if (imgFile.PhysicalDimension.Width > 500 || imgFile.PhysicalDimension.Height > 500)
  {
      cvDeviceImage.IsValid = false;
      fupDeviceImage.Focus();
      return;
  }

#3


In this Code you can see that uploaded image with their height and width and Convert it in to 400 to 800 pixel approximate*

在此代码中,您可以看到上传的图像的高度和宽度,并将其转换为400到800像素近似*

![First of all you need these server controls to Upload and display image ...that is show in this link ][1]

![首先,您需要这些服务器控件来上传和显示图像...在此链接中显示] [1]

[1]: http://i.stack.imgur.com/X0wNJ.png

You also need one panel server control to add Image in page dynamically;

您还需要一个面板服务器控件来动态地在页面中添加Image;

    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>

Then add in the Asp.cs class code for the Button1_click:

然后为Button1_click添加Asp.cs类代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = FileUpload1.FileName + "<br>" + FileUpload1.PostedFile.ContentType + "<br>" + FileUpload1.PostedFile.ContentLength;
        string s = Request.MapPath(Request.ApplicationPath + "/upload/" + FileUpload1.FileName);
        try
        {
            FileUpload1.SaveAs(s);
            Label1.Text += "<br>file upload success..";
            //For Get Uploaded Image height and Width :-
            System.Drawing.Image im = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
            double h = im.PhysicalDimension.Height;
            double w = im.PhysicalDimension.Width;
            Label2.Text = "Height:" + h + "...Widht: " + w;
            //For Display a Image in Panal control and set it height and width:-
            Image img = new Image();
            img.ImageUrl = "/upload/" + FileUpload1.FileName;
            if (h > w)
            {
                while (h > 800)
                {
                    h = h * 0.5;
                }
            }
            else
            {
                while (h > 400)
                {
                    h = h * 0.5;
                }
            }
            img.Height = new Unit(h);
            if (w > h)
            {
                while (w > 800)
                {
                    w = w * 0.5;
                }
            }
            else
            {
                while (w > 400)
                {
                    w = w * 0.5;
                }
            }
            img.Width = new Unit(w);
            Panel1.Controls.Add(img);
            Label2.Text += "<br>Now..Height:" + h + "...Widht: " + w;
        }
        catch
        {
            Label2.Text = "<br>choose your file..";
            Label1.Text = "";
        }
    }