【问题标题】:Crop an image in asp.net using c#使用 c# 在 asp.net 中裁剪图像
【发布时间】:2012-04-18 05:25:09
【问题描述】:

我正在使用 c# 开发 asp.net。我必须通过拍摄该图像的一部分来调整图像。 我想从中间裁剪一部分图像,如下图所示。

谁能帮帮我。

【问题讨论】:

标签: c# jquery asp.net image crop


【解决方案1】:

如果你在服务器上做,我建议使用a server-safe wrapper 而不是直接使用System.Drawing so you don't have to worry about avoiding the 29+ pitfalls and bugs

我的ImageResizing.Net library offers both automatic and manual cropping

自动

new ImageJob(source,dest,new 
 ResizeSettings("width=200;height=200;mode=crop;anchor=middlecenter")).Build();

手动(按百分比)

new ImageJob(source,dest,new 
 ResizeSettings("crop=20,20,80,80;cropxunits=100;cropyunits=100")).Build();

手动(在源图像坐标中)

new ImageJob(source,dest,new 
 ResizeSettings("crop=200,200,1000,1000;")).Build()

【讨论】:

    【解决方案2】:

    我通过 Jquery 获取图像部分的坐标来做到这一点。

    jQuery(function($) {
            $('#target').Jcrop({
                onChange: showCoords,
                onSelect: showCoords,
                onRelease: clearCoords
            });
        });
        function showCoords(c) {
            $('#xaxis').val(c.x);
            $('#yaxis').val(c.y);
            $('#x2').val(c.x2);
            $('#y2').val(c.y2);
            $('#xwidth').val(c.w);
            $('#div_width').val(c.w);
            $('#yheight').val(c.h);
            $('#div_height').val(c.h);
        };
        function clearCoords() {
            $('#coords input').val('0');
            $('#yheight').css({ color: 'red' });
            window.setTimeout(function() {
                $('#yheight').css({ color: 'inherit' });
            }, 500);
        };
    

    然后我使用这些坐标在 C# 中裁剪图像

    String savedFileName = uploadProfileImage(profileImageName, new System.Drawing.Rectangle(Int32.Parse(xaxis), Int32.Parse(yaxis), Int32.Parse(xwidth), Int32.Parse(yheight)));
    
    public String uploadProfileImage(string profileImageName, System.Drawing.Rectangle rectangle)
        {
            try
            {
                String retFileName = "";
                if (profileImageName != null || profileImageName != "")
                {
                    GenerateCroppedThumbNail(profileImageName, rectangle);
                }
                return retFileName;
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }
    

    效果很好

    【讨论】:

    • GenerateCroppedThumbNail 有什么作用?为什么不将其显示为答案的一部分,因为它似乎是最重要的部分
    • 我同意@link64
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2010-10-18
    相关资源
    最近更新 更多