上传图像时出错。 [英] Error While Uploading the image.

查看:73
本文介绍了上传图像时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





上传项目中的图片...



上传时..



错误发生



1.''''''''''''''''''''''''''''''''''''' .Web.UI.WebControls.Image''和''System.Drawing.Image''



2。''System.Web.UI.WebControls.Image' ''不包含''FromStream'的定义'





在上升时,我正在减小图像的大小..



代码

Hi,

am uploading the image in project...

while uploading..

Errors are occured

1.''Image'' is an ambiguous reference between ''System.Web.UI.WebControls.Image'' and ''System.Drawing.Image''

2.''System.Web.UI.WebControls.Image'' does not contain a definition for ''FromStream''


while uploding , i am reducing the size of image..

code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class AdminControl : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel3.Visible = false;
            Bind();
            BindDataList();
        }
    }

    protected void BindDataList()
    {
        DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
        FileInfo[] files = dir.GetFiles();
        ArrayList listItems = new ArrayList();
        foreach (FileInfo info in files)
        {
            listItems.Add(info);
        }
        //dtlist.DataSource = listItems;
        //dtlist.DataBind();

    }

    protected void Bind()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from EanatomyCategory", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        ddlanatomyCate.DataSource = dt;
        ddlanatomyCate.DataTextField = "AnatomyName";
        ddlanatomyCate.DataValueField = "ID";
        ddlanatomyCate.DataBind();
        ddlanatomyCate.Items.Insert(0, new ListItem("-Select Anatomy-", "0"));
    }


    public static int ID;
    public static string ICON;
    public static int Result;


    protected void lnkaddmore_Click(object sender, EventArgs e)
    {
        Panel4.Visible = false;
        Panel1.Visible = false;
        Panel3.Visible = true;
    }
    protected void btnSubmit1_Click(object sender, EventArgs e)
    {
        String extention;
        String name = iconuplaod.FileName;
        string filename2 = Path.GetFileName(ScreenShot1.PostedFile.FileName);
        String name1 = name.ToUpper();

        if (name1.EndsWith(".JPG"))
            extention = ".JPG";
        else if (name1.EndsWith(".GIF"))
            extention = ".GIF";
        else if (name1.EndsWith(".JPEG"))
            extention = ".JPEG";
        else if (name1.EndsWith(".gif"))
            extention = ".gif";
        else
            extention = "";


        String filename = Path.GetFileName(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Today.Minute.ToString() + DateTime.Now.Second.ToString() + extention);

        iconuplaod.SaveAs(Server.MapPath("Images/" + filename));
        ScreenShot1.SaveAs(Server.MapPath("ScreenShot/" + filename2));

        int CatId = ddlanatomyCate.SelectedIndex;
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into AdminMaster(ID,A_icon)values(@ID,@A_icon)", con);

        ID = CatId;
        ICON = filename;

        cmd.Parameters.AddWithValue("@ID", CatId);
        cmd.Parameters.AddWithValue("@A_icon", filename);
        cmd.ExecuteNonQuery();

        

        string targetPath = Server.MapPath("Images/" + filename);
        Stream strm = iconuplaod.PostedFile.InputStream;
        var targetFile = targetPath;
        //Based on scalefactor image size will vary
        GenerateThumbnails(0.5, strm, targetFile);
       

        //Find Last Inserted ID//

        cmd = new SqlCommand("Select @@Identity", con);
        Result = Convert.ToInt32(cmd.ExecuteScalar());

        SqlCommand cmd1 = new SqlCommand("Insert into AdminTransaction (A_id,ID,title,ScreenShot,Description)values(@A_id1,@ID1,@More_title1,@More_screenshot2,@More_desc2) ", con);
        cmd1.Parameters.AddWithValue("@A_id1", Result);
        cmd1.Parameters.AddWithValue("ID1", ID);
        cmd1.Parameters.AddWithValue("@More_title1", txttitle1.Text);
        cmd1.Parameters.AddWithValue("@More_screenshot2", filename2);
        cmd1.Parameters.AddWithValue("More_desc2", txtdesc1.Text);

        cmd1.ExecuteNonQuery();
        con.Close();
        //SavMoreDetails();
        clear();
    }

    // 
    private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
    {
        using (var image = Image.FromStream(sourcePath))
        {
            var newWidth = (int)(160 * scaleFactor);
            var newHeight = (int)(160 * scaleFactor);
            var thumbnailImg = new Bitmap(newWidth, newHeight);
            var thumbGraph = Graphics.FromImage(thumbnailImg);
            thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbnailImg.Save(targetPath, image.RawFormat);
        }
    }
    protected void btnSubmit2_Click(object sender, EventArgs e)
    {
        SavMoreDetails();
        clear();
    }

    public void SavMoreDetails()
    {
        //string filename3 = Path.GetFileName(Screenshot2.PostedFile.FileName);
        string filename3 = Screenshot2.FileName;
        Screenshot2.SaveAs(Server.MapPath("ScreenShot/" + filename3));

        con.Open();

        SqlCommand cmd = new SqlCommand("Insert into AdminTransaction(A_id,ID,title,ScreenShot,Description)values(@A_id,@ID,@More_title,@More_screenshot1,@More_desc1) ", con);


        cmd.Parameters.AddWithValue("@A_id", Result);
        cmd.Parameters.AddWithValue("ID", ID);
        cmd.Parameters.AddWithValue("@More_title", txttiltel2.Text);
        cmd.Parameters.AddWithValue("@More_screenshot1", filename3);
        cmd.Parameters.AddWithValue("More_desc1", txtdesc2.Text);

        cmd.ExecuteNonQuery();
        con.Close();

    }

    public void clear()
    {
        txttitle1.Text = "";
        txtdesc1.Text = "";
        txttiltel2.Text = "";
        txtdesc2.Text = "";
    }



    protected void lnkaddmore1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}

推荐答案

''图像''是''System.Web之间的模糊参考。 UI.WebControls.Image''和''System.Drawing.Image''意味着你的代码不知道你在代码中引用了哪个类。

你可以通过声明来明确告诉你的变量使用整个路径(命名空间)到你的班级:



''Image'' is an ambiguous reference between ''System.Web.UI.WebControls.Image'' and ''System.Drawing.Image'' means that your code doesn''t know which class you are refereng to in your code.
You can explicitly tell by declaring your variable using the whole path (namespace) to your class:

using (var image = System.Drawing.Image.FromStream(sourcePath))


  1. 不要写类型 Image 。写全名; System.Web.UI.WebControls.Image System.Drawing.Image ,具体取决于你真正需要的那个:

    http://msdn.microsoft .com / zh-cn / library / system.web.ui.webcontrols.image.aspx [ ^ ],

    http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx [ ^ ]。
  2. 此错误与错误#1有关。如果您确实需要这两种类型中的第二种并使用它的完全限定名称命名,则编译器将正确识别它们。它确实有您尝试使用的方法:

    http:// msdn。 microsoft.com/en-us/library/93z9ee4x.aspx [ ^ ]。
  1. Don''t write the type Image. Write the full name; either System.Web.UI.WebControls.Image or System.Drawing.Image, depending on which one you really need:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.aspx[^],
    http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^].
  2. This error is related to the error #1. If you really need the second of the two types and name it with it fully-qualified name, the will be recognized by the compiler correctly. It does have the method you tried to use:
    http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx[^].





这就是全部。不幸的是,你不了解一些.NET基础知识。在回到ASP.NET开发和其他高级事物之前,也许你需要退后一步学习才能获得足够的信心。



- SA


您可以使用类型别名...

参考 - 两个NameSpaces之间的模糊引用 [ ^ ]
You can use a type alias...
Refer - Ambiguous Reference Between Two NameSpaces[^]


这篇关于上传图像时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆