当前位置:编程学习 > C#/ASP.NET >>

C#获取上传图片尺寸

 imgPath = FileUpload1.PostedFile.FileName.ToString();
            System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
            string hig = image.Height.ToString();
            string wid = image.Width.ToString();
提示出错信息:错误 1 当前上下文中不存在名称“imgPath”
请高手帮忙看看是什么问题啊? --------------------编程问答-------------------- 你要把图片写上传到服务器上再读

 byte[] bytes = new byte[this.FileUpload1.PostedFile.ContentLength];
            FileUpload1.PostedFile.InputStream.Read(bytes, 0, bytes.Length);
            FileStream fs = new FileStream(@"c:\img", FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            FileStream read = new FileStream(@"c:\img", FileMode.Open);
            System.Drawing.Image img = System.Drawing.Image.FromStream(read);
            string hig = img.Height.ToString();
            string wid = img.Width.ToString();
            read.Close(); --------------------编程问答-------------------- 你这个imgPath是在什么地方定义的? --------------------编程问答-------------------- 1 当前上下文中不存在名称“imgPath”
------------------------------------
变量定义出问题了吧,分清定义的位置不同,变量的作用不同 --------------------编程问答-------------------- 两个一样的帖子啊 --------------------编程问答-------------------- imgPath好像只是文件的名字
还没有save,
试试save后有了实际文件且有路径了是不是可以实例化Image了? --------------------编程问答-------------------- 楼主急了吧~发这么多贴~用我的吧,
这是类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

/// <summary>
/// ImageThumbnail 的摘要说明
/// </summary>
public class ImageThumbnail
{
    public Image ResourceImage;
    private int ImageWidth;
    private int ImageHeight;

    public string ErrorMessage;


    public ImageThumbnail(Stream ImageFileName)
    {

        ResourceImage = Image.FromStream(ImageFileName);
        ErrorMessage = "";

    }
    public bool ThumbnailCallback()
    {
        return false;
    }
    // 方法1,按大小
    public bool ReducedImage(int Width, int Height, string targetFilePath)
    {


        try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

            int w = ResourceImage.Width;
            int h = ResourceImage.Height;

            if (w < Width && h < Height)
            {
                Width = w;
                Height = h;
            }
            else
            {
                if (h * Width / w > Height)
                {
                    Width = w * Height / h;
                }
                else
                {
                    Height = h * Width / w;
                }
            }
            
            ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return false;
        }

    }


    // 方法2,按百分比  缩小60% Percent为0.6 targetFilePath为目标路径
    public bool ReducedImage(double Percent, string targetFilePath)
    {
        try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
            ImageHeight = (ResourceImage.Height) * ImageWidth / ResourceImage.Width;//等比例缩放
            ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return false;
        }
    }


}



这是方法例子,我存了二张图片,看清楚,你急就将就用吧

 if (FileUpload2.HasFile)
            {

                if (FileUpload2.PostedFile.ContentLength > 0)
                {
                    bool FilesOK = false;
                    String fileExtension = Path.GetExtension(FileUpload2.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg", ".wmv", ".rmvb", ".rm" };

                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        { FilesOK = true; }
                    }
                    if (FilesOK)
                    {
                        FileName2 = "1" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + new Random().Next(0, 100000000).ToString() + fileExtension;

                        Stream filepath1 = FileUpload2.PostedFile.InputStream;
                        string serverpath2 = Server.MapPath("~/upload/s") + FileName2;
                        string serverpath3 = Server.MapPath("~/upload/b") + FileName2;
                        ImageThumbnail img = new ImageThumbnail(filepath1);
                        // img.ReducedImage(0.4, serverpath2);//0.4表示缩小40%
                        img.ReducedImage(242, 138, serverpath2);
                        img.ReducedImage(760, 510, serverpath3);
                        LawsTime = DateTime.Now;
                        PD._PPicIndexName = FileName2;
                        PD._PLawsTime = LawsTime;
                        PD._PIsIndex = Convert.ToInt32(this.rbIsIndex.SelectedValue);
                        filepath1.Close();
                    }
                    else
                    {
                        Response.Write(MaginKey.Alert("图片格式不正确!"));
                    }
                }
            }
            else
            {

                Response.Write(MaginKey.Alert("首页图片不能为空!"));
            }
--------------------编程问答-------------------- 别忘了引用这些
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,