我如何获得身高和身高 上传图像文件的宽度

发布于 2024-07-25 09:17:24 字数 258 浏览 6 评论 0原文

我正在使用 ASP.NET 2.0。 我正在尝试获取高度和高度 我使用 FileUpload 控件上传的图像文件的宽度。 上传后,图像将保存在数据库中,而不是文件系统中。 看来我应该能够使用类似以下代码的东西来执行此操作,但我无法让它工作。

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-08-01 09:17:24

我已经找到了解决方案。

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

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

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
长不大的小祸害 2024-08-01 09:17:24

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

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;
  }

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;
  }
江城子 2024-08-01 09:17:24

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

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

[1]:https://i.sstatic.net/X0wNJ.png


您还需要一个面板服务器控件来动态添加图像到页面中;

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

然后在Asp.cs类中添加Button1_click的代码:

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 = "";
        }
    } 

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

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

[1]: https://i.sstatic.net/X0wNJ.png

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

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

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

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 = "";
        }
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文