2015-04-05 45 views
0

我一直在敲我的头近三天now.THE以下代码工作精美,没有MASTERPAGE.But当我复制粘贴整个东西在主页(哪里有MASTERPAGE),它不起作用,并发出错误“输入字符串格式不正确”。Jcrop只能在表格标签内工作

我猜测代码只有当我把它包含在表单标签中时才起作用,但是我不能在一个contentplaceholder中有一个表单标签。我该如何解决这个问题?

btnUpload工作正常,但当我点击btnCrop时弹出错误。 的HTML是如下:

<form id="form1" runat="server"> 
<div style="width: 540px" runat="server"> 
     <fieldset> 
     <legend>Upload, crop and save image in asp.net</legend> 
     <table> 
      <tr> 
       <td> 
        Select image to upload : 
       </td> 
       <td> 
        <asp:FileUpload ID="FileUpload1" runat="server" /> 
       </td> 
       <td> 
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="3"> 
        <asp:Image ID="imgCropped" Width="172px" Height="172px" runat="server" /> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="3"> 
        <asp:Label ID="lblMsg" runat="server" ForeColor="Red" /> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="3"> 
        <asp:Button ID="btnReset" runat="server" Text="Reset" Visible="false" OnClick="btnReset_Click" /> 
       </td> 
      </tr> 
     </table> 
     <asp:Panel ID="pnlCrop" runat="server" Visible="false"> 
      <table> 
       <tr> 
        <td> 
         <asp:Image ID="imgToCrop" runat="server" /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <asp:Button ID="btnCrop" runat="server" Text="Crop & Save" OnClick="btnCrop_Click" /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <input id="XCoordinate" type="hidden" runat="server" /> 
         <input id="YCoordinate" type="hidden" runat="server" /> 
         <input id="Width" type="hidden" runat="server" /> 
         <input id="Height" type="hidden" runat="server" /> 
        </td> 
       </tr> 
      </table> 
     </asp:Panel> 
    </fieldset> 
    </div> 
    </form> 

的代码,如果需要的是如下:

protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    string fileName = string.Empty; 
    string filePath = string.Empty; 
    string extension = string.Empty; 
    try 
    { 
     //Check if Fileupload control has file in it 
     if (FileUpload1.HasFile) 
     { 
      // Get selected image extension 
      extension = Path.GetExtension(FileUpload1.FileName).ToLower(); 
      //Check image is of valid type or not 
      if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp") 
      { 
       //Cretae unique name for the file 
       fileName = Guid.NewGuid().ToString() + extension; 
       //Create path for the image to store 
       filePath = Path.Combine(Server.MapPath("/Images"), fileName); 
       //Save image in folder 
       FileUpload1.SaveAs(filePath); 
       //Show the panel and load the uploaded image in image control. 
       pnlCrop.Visible = true; 
       imgToCrop.ImageUrl = "/Images/" + fileName; 
      } 
      else 
      { 
       lblMsg.Text = "Please select jpg, jpeg, png, gif or bmp file only"; 
      } 
     } 
     else 
     { 
      lblMsg.Text = "Please select file to upload"; 
     } 
    } 
    catch (Exception ex) 
    { 
     lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString(); 
    } 
    finally 
    { 
     extension = string.Empty; 
     fileName = string.Empty; 
     filePath = string.Empty; 
    } 
} 

protected void btnCrop_Click(object sender, EventArgs e) 
{ 
    string croppedFileName = string.Empty; 
    string croppedFilePath = string.Empty; 
    //get uploaded image name 
    string fileName = Path.GetFileName(imgToCrop.ImageUrl); 
    //get uploaded image path 
    string filePath = Path.Combine(Server.MapPath("~/Images"), fileName); 

    //Check if file exists on the path i.e. in the UploadedImages folder. 
    if (File.Exists(filePath)) 
    { 
     //Get the image from UploadedImages folder. 
     System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath); 
     //Get user selected cropped area 
     //Convert.ToInt32(String.Format("{0:0.##}", YCoordinate.Value)), 

     Rectangle areaToCrop = new Rectangle(
      Convert.ToInt32(XCoordinate.Value), 
      Convert.ToInt32(YCoordinate.Value), 
      Convert.ToInt32(Width.Value), 
      Convert.ToInt32(Height.Value)); 
     try 
     { 

      Bitmap bitMap = new Bitmap(areaToCrop.Width, areaToCrop.Height); 
      //Create graphics object for alteration 
      using (Graphics g = Graphics.FromImage(bitMap)) 
      { 
       //Draw image to screen 
       g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), areaToCrop, GraphicsUnit.Pixel); 
      } 

      //name the cropped image 
      croppedFileName = "crop_" + fileName; 
      //Create path to store the cropped image 
      croppedFilePath = Path.Combine(Server.MapPath("~/Images"), croppedFileName); 
      //save cropped image in folder 
      bitMap.Save(croppedFilePath); 
      orgImg.Dispose(); 
      bitMap = null; 
      //Now you can delete the original uploaded image from folder     
      File.Delete(filePath); 
      //Hide the panel 
      pnlCrop.Visible = false; 
      //Show success message in label 
      lblMsg.ForeColor = Color.Green; 
      lblMsg.Text = "Image cropped and saved successfully"; 

      //Show cropped image 
      imgCropped.ImageUrl = "~/Images/" + croppedFileName; 
      string CS = ConfigurationManager.ConnectionStrings["SportsActiveConnectionString"].ConnectionString; 
      using (SqlConnection con = new SqlConnection(CS)) 
      { 
       con.Open(); 
       SqlCommand cmd = new SqlCommand("spLogo", con); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.AddWithValue("@LogoPath", "Images/" + croppedFileName); 
       cmd.ExecuteNonQuery(); 
      } 
      //Show Reset button 
      btnReset.Visible = true; 
     } 
     catch (Exception ex) 
     { 
      lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString(); 
     } 
     finally 
     { 
      fileName = string.Empty; 
      filePath = string.Empty; 
      croppedFileName = string.Empty; 
      croppedFilePath = string.Empty; 
     } 
    } 

} 
+0

你有没有调试你的代码?您必须在'Rectangle areaToCrop ='行获得错误,并且如果此代码位于contentplaceholder内,那么您的母版页必须具有表单标记,这就够了。 – 2015-04-05 07:05:56

+0

是的,我得到错误“输入字符串不正确的格式”在该行 – RelatedRhymes 2015-04-05 07:07:16

+0

这就是我说的,当我从上面的代码中删除表单标签,它会引发错误。当我将上面的代码粘贴到页面没有masterpage和contentplaceholder,它的工作原理。但我想使用一个页面,有一个主页页面的代码以及contentplaceholder – RelatedRhymes 2015-04-05 07:09:21

回答

0

原因你的错误是转化率从XCoordinate.ValueXCoordinate.Value等价值的integer。在提供的字符串中可以有非整数值的转换时,您应该保持谨慎。

使用TryParse相反: -

int xCoordinate; 
if(int.TryParse(XCoordinate.Value,out xCoordinate) 
     //your code 

编辑: 时转换一个字符串为整数失败,出现此错误,的TryParse方法,如果转换成功,否则为false,如果转换成功返回true ,它将转换的值分配给我们在方法中传递的输出变量(在这种情况下为xCoordinate)。

int xCoordinate,yCoordinate,width,height; 
if(int.TryParse(XCoordinate.Value,out xCoordinate) && 
    int.TryParse(YCoordinate.Value,out yCoordinate) && 
    int.TryParse(Width.Value,out width) && int.TryParse(Height.Value,out height)) 
{ 
    Rectangle areaToCrop = new Rectangle(xCoordinate,yCoordinate,width,height) 
} 
else 
{ 
    //Throw error, use any default values etc. 
} 
+0

那么它是如何工作在页面上没有一个母版页? – RelatedRhymes 2015-04-05 07:13:16

+0

我不知道如何在我的代码中实现上述所有坐标。请帮助我与 – RelatedRhymes 2015-04-05 07:15:49

+0

@RelatedRhymes - 我不确定它为什么不与母版页一起工作,但是这个问题的原因是仅转换,请检查我的更新,并且可以参考MSDN文档获取更多信息。 – 2015-04-05 07:21:23

0

当你与ID XCoordinate整合你的aspx input到您的母版页输入的ID发生变化形式XCoordinateContentPlaceHolder1_XCoordinate。由于这个问题隐藏的输入没有值,因此你的后端代码不能将空字符串解析为Int。

去替代你的脚本是:

jQuery(document).ready(function() { 
     jQuery('#<%= imgToCrop.ClientID %>').Jcrop({ 
      onSelect: updateCoords 
     }); 
    }); 

    function updateCoords(c) { 
     jQuery('#<%= XCoordinate.ClientID %>').val(c.x); 
     jQuery('#<%= YCoordinate.ClientID %>').val(c.y); 
     jQuery('#<%= Width.ClientID %>').val(c.w); 
     jQuery('#<%= Height.ClientID %>').val(c.h); 
    }; 
+0

仍然是一样的错误。但是,它并不在masterpage中,它在一个webpage中有一个masterpage。 – RelatedRhymes 2015-04-05 08:47:14

+0

@Bhavik - 我同意客户端ID将会按照您演示的方式进行更改,但与OP的错误消息无关,因为他正在处理服务器端代码而不是客户端代码! – 2015-04-05 09:36:53

相关问题