单击裁剪按钮后显示错误...参数无效且源错误为第113行:catch(Exception Ex)第114行:{第115行:throw(Ex);第116行:}第117行:...... [英] After clicking on the crop button its showing the error ...Parameter is not valid And the source error is Line 113: catch (Exception Ex) Line 114: { Line 115: throw (Ex); Line 116: } Line 117:...

查看:161
本文介绍了单击裁剪按钮后显示错误...参数无效且源错误为第113行:catch(Exception Ex)第114行:{第115行:throw(Ex);第116行:}第117行:......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <%@     Page    语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  UCrop.aspx.cs   继承  =  WebCrop.UCrop   %>  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title >
< link href = css / jquery.Jcrop.css rel = 样式表 / >
< script src = scripts / jquery.Jcrop.pack.js > < / script >
< script src = scripts / jquery.min.js > < / script >
< script src = scripts / jquery.Jcrop.js > < / script >
< script src = scripts / jquery.color.js > < / script >
< script src = scripts / jquery.Jcrop.min.js > < / script >
< link href = css / jquery.Jcrop.min.css rel = styl esheet / >
< script >
$(document).ready(function()
{

$( #imgCrop)。Jcrop({
onSelect:storeCoords
});
});

function storeCoords(c){
$( #X)VAL(CX)。
$( #Y)。val(c.y);
$( #W)。val(c.w);
$( #H)。val(c.h);

};

< / script >
< / head >
< body >
< 表单 id = form1 runat = server >
< div >
< < /跨度> asp:Panel ID = pnlUpload runat = server >
< asp:FileUpload ID = 上传 runat = < span class =code-keyword> server
/ >
< br / >
< asp:按钮 ID = btnUpload runat = server OnClick = btnUpload_Click 文字 = 上传 / >
< asp:标签 ID = lblError runat = server 可见 = false / >
< / asp:Panel > ;
< asp:Panel ID = pnlCrop runat = server 可见 < span class =code-keyword> =
false >
< asp:Image ID = imgCrop runat = 服务器 高度 = 258px Wi dth = 285px / >
< asp:按钮 ID = Button1 runat = server 文字 = 裁剪 OnClick = btnCrop_Click / >
< asp:HiddenField ID = X runat = server = 0 / < span class =code-keyword>>

< asp:HiddenField ID = Y runat = server Value = 0 / >
< asp:HiddenField ID = W runat < span class =code-keyword> = server = 0 / >
< asp:HiddenField ID = H runat = server = 0 / >
< / asp:Panel >
< asp:Panel ID = pnlCropped runat = server 可见 = false >
< asp:Image ID = < span class =code-keyword> imgCropped runat = server / >
< / asp:面板 >
< / div >
< / form >
< / body >
< / html >



代码隐藏文件

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.IO;
使用 SD = System.Drawing;
使用 System.Drawing.Imaging;
使用 System.Drawing.Drawing2D;

命名空间 WebCrop
{
public partial class UCrop:System.Web.UI.Page
{
字符串 path = HttpContext.Current.Request.PhysicalApplicationPath + images \\\ \\;

受保护 void Page_Load(对象发​​件人,EventArgs e)
{

}

受保护 void btnUpload_Click( object sender,EventArgs e)
{
布尔值 FileOK = false ;
布尔 FileSaved = false ;
if (Upload.HasFile)
{
Session [ WorkingImage] = Upload.FileName;
字符串 FileExtension = Path.GetExtension(会话[ WorkingImage]的ToString())ToLower将();
字符串 [] allowedExtensions = { 。 png .jpeg 。jpg 。gif };
for int i = 0 ; i < allowedExtensions.Length; i ++)
{
if (FileExtension == allowedExtensions [i])
{
FileOK = true ;
}
}
}

如果(FileOK)
{
尝试
{
Upload.PostedFile.SaveAs(路径+会话[ < span class =code-string> WorkingImage
]);
FileSaved = true ;
}
catch (例外情况)
{
lblError.Text = 无法上传文件。 + ex.Message.ToString();
lblError.Visible = true ;
FileSaved = false ;
}
}
else
{
lblError.Text = 无法接受此类型的文件。;
lblError.Visible = true ;
}

if (FileSaved)
{
pnlUpload.Visible = ;
pnlCrop.Visible = true ;
imgCrop.ImageUrl = images / + Session [ WorkingImage]。ToString();
}
}
受保护 void btnCrop_Click( object sender,EventArgs e)
{
string ImageName = Session [ WorkingImage]。ToString();
int h1 = Convert.ToInt32(X.Value);
int h2 = Convert.ToInt32(Y.Value);
int h3 = Convert.ToInt32(W.Value);
int h4 = Convert.ToInt32(H.Value);

byte [] CropImage = Crop(路径+ ImageName,h1,h2,h3,h4);
使用(MemoryStream ms = new MemoryStream(CropImage, 0 ,CropImage.Length))
{
ms.Write(CropImage, 0 ,CropImage.Length);
使用(SD.Image CroppedImage = SD.Image.FromStream(ms, true ))
{
string SaveTo = path + crop + ImageName;
CroppedImage.Save(SaveTo,CroppedImage.RawFormat);
pnlCrop.Visible = false ;
pnlCropped.Visible = true ;
imgCropped.ImageUrl = crop + ImageName;
}
}
}

静态 byte []裁剪(字符串 Img, int X1, int Y1, int X2, int Y2)
{
尝试
{
使用(SD.Image OriginalImage = SD.Image。 FromFile(Img))
{
使用(SD.Bitmap bmp = new SD.Bitmap(X2,Y2))
{
bmp.SetResolution(OriginalImage.Horizo​​ntalResolution,OriginalImage.VerticalResolution);
使用(SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias ;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new SD.Rectangle( 0 0 ,X2,Y2),X1,Y1,X2,Y2,SD.GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms,OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Ex Ex)
{
throw (Ex);
}
}
}
}

解决方案

(document).ready( function()
{


#imgCrop)。Jcrop({
onSelect:storeCoords
});
});

function storeCoords(c){


#X)VAL(CX);

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UCrop.aspx.cs" Inherits="WebCrop.UCrop" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title></title>
    <link href="css/jquery.Jcrop.css" rel="stylesheet" />
    <script src="scripts/jquery.Jcrop.pack.js"></script>
    <script src="scripts/jquery.min.js"></script>
    <script src="scripts/jquery.Jcrop.js"></script>
    <script src="scripts/jquery.color.js"></script>
    <script src="scripts/jquery.Jcrop.min.js"></script>
    <link href="css/jquery.Jcrop.min.css" rel="stylesheet" />
    <script>
        $(document).ready(function()
        {

            $("#imgCrop").Jcrop({
                onSelect: storeCoords
            });
        });

        function storeCoords(c) {
            $("#X").val(c.x);
            $("#Y").val(c.y);
            $("#W").val(c.w);
            $("#H").val(c.h);

        };

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Panel ID="pnlUpload" runat="server">
      <asp:FileUpload ID="Upload" runat="server" />
      <br />
      <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />
      <asp:Label ID="lblError" runat="server" Visible="false" />
    </asp:Panel>
    <asp:Panel ID="pnlCrop" runat="server" Visible="false">
      <asp:Image ID="imgCrop" runat="server" Height="258px" Width="285px" />
         <asp:Button ID="Button1" runat="server" Text="Crop" OnClick="btnCrop_Click" />
      <asp:HiddenField ID="X" runat="server" Value="0"/>
      <asp:HiddenField ID="Y" runat="server" Value="0"/>
      <asp:HiddenField ID="W" runat="server" Value="0"/>
      <asp:HiddenField ID="H" runat="server" Value="0"/>
    </asp:Panel>
    <asp:Panel ID="pnlCropped" runat="server" Visible="false">
      <asp:Image ID="imgCropped" runat="server" />
    </asp:Panel>
    </div>
    </form>
</body>
</html>


Codebehind File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using SD = System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WebCrop
{
    public partial class UCrop : System.Web.UI.Page
    {
        String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
             Boolean FileOK = false;
             Boolean FileSaved = false;
  if (Upload.HasFile)
  {
    Session["WorkingImage"] = Upload.FileName;
    String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower();
    String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
      if (FileExtension == allowedExtensions[i])
      {
        FileOK = true;
      }
    }
  }

  if (FileOK)
  {
    try
    {
      Upload.PostedFile.SaveAs(path + Session["WorkingImage"]);
      FileSaved = true;
    }
    catch (Exception ex)
    {
      lblError.Text = "File could not be uploaded." + ex.Message.ToString();
      lblError.Visible = true;
      FileSaved = false;
    }
  }
       else
      {
    lblError.Text = "Cannot accept files of this type.";
    lblError.Visible = true;
       }

       if (FileSaved)
       {
    pnlUpload.Visible = false;
    pnlCrop.Visible = true;
    imgCrop.ImageUrl = "images/" + Session["WorkingImage"].ToString();
      }
}
protected void btnCrop_Click(object sender, EventArgs e)
        {
            string ImageName = Session["WorkingImage"].ToString();
            int h1 = Convert.ToInt32(X.Value);
            int h2 = Convert.ToInt32(Y.Value);
            int h3 = Convert.ToInt32(W.Value);
            int h4 = Convert.ToInt32(H.Value);

                byte[] CropImage = Crop(path + ImageName, h1, h2, h3, h4);
                using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
                {
                    ms.Write(CropImage, 0, CropImage.Length);
                    using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
                    {
                        string SaveTo = path + "crop" + ImageName;
                        CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
                        pnlCrop.Visible = false;
                        pnlCropped.Visible = true;
                        imgCropped.ImageUrl = "crop" + ImageName;
                    }
                }
            }

        static byte[] Crop(string Img, int X1, int Y1,int X2, int Y2 )
        {
            try
            {
                using (SD.Image OriginalImage = SD.Image.FromFile(Img))
                {
                    using (SD.Bitmap bmp = new SD.Bitmap(X2, Y2))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, X2, Y2), X1, Y1, X2, Y2, SD.GraphicsUnit.Pixel);
                            MemoryStream ms = new MemoryStream();
                            bmp.Save(ms, OriginalImage.RawFormat);
                            return ms.GetBuffer();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
        }
        }
    }

解决方案

(document).ready(function() {


("#imgCrop").Jcrop({ onSelect: storeCoords }); }); function storeCoords(c) {


("#X").val(c.x);


这篇关于单击裁剪按钮后显示错误...参数无效且源错误为第113行:catch(Exception Ex)第114行:{第115行:throw(Ex);第116行:}第117行:......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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