复制代码 代码如下:
<link href="Styles/jquery.Jcrop.css" type="text/css" />
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.Jcrop.js" type="text/javascript"></script>
复制代码 代码如下:
<asp:Label Text="原始图片" runat="server"></asp:Label><br />
<asp:Image runat="server" />
<br />
<asp:Label runat="server" Text="最终显示效果"></asp:Label>
<div>
<asp:Image runat="server" />
</div>
复制代码 代码如下:
$('#target').Jcrop({
onChange: updatePreview,
onSelect: updatePreview,
onRelease: clearCoords,
aspectRatio: 150 / 80,
minSize: _minarray,
setSelect: _array
}, function () {
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
jcrop_api = this;
});
//此方法是用来及时展现图片裁剪效果
function updatePreview(c) {
if (parseInt(c.w) > 0) {
var rx = 150 / c.w;
var ry = 80 / c.h;
var _width;
var _height;
if (Math.round(rx * boundx) > $targetImg.width()) {
_width = $targetImg.width();
}
else {
_width = Math.round(rx * boundx);
}
if (Math.round(ry * boundy) > $targetImg.height()) {
_height = $targetImg.height();
}
else {
_height = Math.round(ry * boundy);
}
$('#preview').css({
width: _width + 'px',
height: _height + 'px',
marginLeft: '-' + Math.round(rx * c.x) + 'px',
marginTop: '-' + Math.round(ry * c.y) + 'px'
});
}
$('#x1').val(c.x);
$('#y1').val(c.y);
$('#Iwidth').val(c.w);
$('#Iheight').val(c.h);
};
复制代码 代码如下:
<form runat="server">
<asp:HiddenField runat="server" />
<asp:HiddenField runat="server" />
<asp:HiddenField runat="server" />
<asp:HiddenField runat="server" />
<asp:HiddenField runat="server" />
<br />
<asp:Button runat="server" Text="裁剪并保存图片" OnClientClick="return CheckIMG()" />
</form>
复制代码 代码如下:
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Design;
复制代码 代码如下:
protected void saveImg(object sender, EventArgs e)
{
if (IsPostBack)
{
string tempurl = Path.Combine(ConfigAccess.UploadImagePath, _url);
int startX = int.Parse(x1.Value);
int startY = int.Parse(y1.Value);
int width = int.Parse(Iwidth.Value);
int height = int.Parse(Iheight.Value);
ImgReduceCutOut(startX, startY, width, height, tempurl, tempurl);
this.target.Visible = false;
this.Label1.Visible = false;
this.SaveImg.Enabled = false;
}
}
复制代码 代码如下:
//通过连接创建Image对象
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgUrl);
oldimage.Save(Server.MapPath("temp.jpg"));//把原图Copy一份出来,然后在temp.jpg上进行裁剪,最后把裁剪后的图片覆盖原图 oldimage.Dispose();//一定要释放临时图片,要不之后的在此图上的操作会报错,原因冲突 Bitmap bm = new Bitmap(Server.MapPath("temp.jpg"));
//处理JPG质量的函数
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
{
ici = codec;
break;
}
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);
// 裁剪图片
Rectangle cloneRect = new Rectangle(startX, startY, int_Width, int_Height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
if (int_Width > int_Standard_Width)
{
//缩小图片
System.Drawing.Image cutImg = cloneBitmap.GetThumbnailImage(int_Standard_Width, int_Standard_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
cutImg.Save(out_ImgUrl, ici, ep);
cutImg.Dispose();
}
else
{
//保存图片
cloneBitmap.Save(out_ImgUrl, ici, ep);
}
cloneBitmap.Dispose();
bm.Dispose();
}
public bool ThumbnailCallback()
{
return false;
}
新闻热点
疑难解答
图片精选