不裁剪所选区域

发布于 2024-12-26 00:25:06 字数 2514 浏览 5 评论 0原文

现在是裁剪部分:以正确的宽度、高度显示图像的不同部分。

要裁剪的区域

在此处输入图像描述

裁剪区域 在此处输入图像描述

这是我的 js 代码

        jQuery(document).ready(function () {

            jQuery('#imgCrop').Jcrop({

                onSelect: storeCoords,
                onChange: storeCoords 


            });

        });

      function storeCoords(c) {

    jQuery('#X').val(c.x);

    jQuery('#Y').val(c.y);

    jQuery('#W').val(c.w);
    jQuery('#H').val(c.h);

  };

protected void btnCrop_Click(object sender, EventArgs e)
{

    string ImageName = Session["WorkingImage"].ToString();

    int w = Convert.ToInt32(W.Value);

    int h = Convert.ToInt32(H.Value);

    int x = Convert.ToInt32(X.Value);

    int y = Convert.ToInt32(Y.Value);



    byte[] CropImage = Crop(path + ImageName, w, h, x, y);

    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 = "images/crop" + ImageName;

        }

    }

}
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{

    try
    {

        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {

            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {

                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, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);

                    MemoryStream ms = new MemoryStream();

                    bmp.Save(ms, OriginalImage.RawFormat);

                    return ms.GetBuffer();

                }

            }

        }

    }

    catch (Exception Ex)
    {

        throw (Ex);

    }

}

}

Now comes the cropping part: Shows a different part of the image with the correct width, height.

Area to be cropped

enter image description here

Cropped Area
enter image description here

Heres my js code

        jQuery(document).ready(function () {

            jQuery('#imgCrop').Jcrop({

                onSelect: storeCoords,
                onChange: storeCoords 


            });

        });

      function storeCoords(c) {

    jQuery('#X').val(c.x);

    jQuery('#Y').val(c.y);

    jQuery('#W').val(c.w);
    jQuery('#H').val(c.h);

  };

protected void btnCrop_Click(object sender, EventArgs e)
{

    string ImageName = Session["WorkingImage"].ToString();

    int w = Convert.ToInt32(W.Value);

    int h = Convert.ToInt32(H.Value);

    int x = Convert.ToInt32(X.Value);

    int y = Convert.ToInt32(Y.Value);



    byte[] CropImage = Crop(path + ImageName, w, h, x, y);

    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 = "images/crop" + ImageName;

        }

    }

}
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{

    try
    {

        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {

            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {

                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, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);

                    MemoryStream ms = new MemoryStream();

                    bmp.Save(ms, OriginalImage.RawFormat);

                    return ms.GetBuffer();

                }

            }

        }

    }

    catch (Exception Ex)
    {

        throw (Ex);

    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-01-02 00:25:06

我曾经遇到过类似的问题,现在记不清了,但我认为您必须在 jCrop 中设置 boxWidth 选项,特别是如果您没有显示图像的原始尺寸(图像实际上是 1024x768但您将裁剪器的样式设置为 350x350)

此外,您可能还必须为 jCrop 设置 aspectRatio,即使它是 01因为它需要根据图像大小重新计算尺寸。
还要确保使用最新版本 0.9.9(截至本文)并检查 JavaScript 内部是否实际上是 0.9.9,因为有时他没有正确更新链接..这让我很困惑...

我想您的服务器代码没问题 - 但您需要检查隐藏字段中设置的数字,以便您可以调试问题。

I had a similar issue once and I cannot remember exactly now but i think you have to set the boxWidth option in the jCrop especially if you are not showing the original size of the image (The image is actually 1024x768 but you style your cropper to 350x350)

Also you might have to set the aspectRatio for jCrop even if it is 0 or 1 because it needs to recalculate the dimensions based on the image size.
Also makes sure to use the latest version 0.9.9 (as of this post) and check inside the JavaScript if it is actually 0.9.9 because some times he does not update the links properly.. It caught me out...

I think your server code is OK- But you need to inspect what are the number getting set in the hidden fields so that you can debug the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文