c# - 图像裁剪无法正常工作

标签 c# image graphics bitmap memorystream

代码片段应该裁剪图像,但是裁剪的大小是正确的,但裁剪从左上角开始。这些图片更清楚地说明了问题herehere

坐标约为:x=68,y=28,宽度=176,高度=174。下面是裁剪代码。

/// <summary>
/// Handles the Click event of the UploadButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
protected void UploadButton_Click(object sender, EventArgs e)
{

    String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";


    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)
        {
            ErrorLabel.Text = "File could not be uploaded." + ex.Message;
            ErrorLabel.Visible = true;
            FileSaved = false;
        }
    }

    else
    {
        ErrorLabel.Text = "Cannot accept files of this type.";
        ErrorLabel.Visible = true;
    }


    if (FileSaved)
    {
        UploadPanel.Visible = false;
        CropPanel.Visible = true;
        CropImage.ImageUrl = "~/Images/" + Session["WorkingImage"];
    }
}

/// <summary>
/// Handles the Click event of the CropButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
/// <remarks></remarks>
protected void CropButton_Click(object sender, EventArgs e)
{
    String path = HttpContext.Current.Request.PhysicalApplicationPath + "Images\\";

    string ImageName = Session["WorkingImage"].ToString();
    int w = Convert.ToInt32(Convert.ToDouble(W.Value));
    int h = Convert.ToInt32(Convert.ToDouble(H.Value));
    int x = Convert.ToInt32(Convert.ToDouble(X.Value));
    int y = Convert.ToInt32(Convert.ToDouble(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 (Image CroppedImage = Image.FromStream(ms, true))
        {
            string SaveTo = path + "crop" + ImageName;
            CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
            CropPanel.Visible = false;
            CroppedPanel.Visible = true;
            this.CroppedImage.ImageUrl = "~/Images/crop" + ImageName;
        }
    }
}

#endregion

#region Methods

/// <summary>
/// Crops the specified image.
/// </summary>
/// <param name="image">The image.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="x">The x value.</param>
/// <param name="y">The y value.</param>
/// <returns></returns>
/// <remarks></remarks>
private static byte[] Crop(string image, int width, int height, int x, int y)
{
    try
    {
        using (Image OriginalImage = Image.FromFile(image))
        {
            using (Bitmap bmp = new Bitmap(width, height))
            {
                //bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                using (Graphics Graphic = Graphics.FromImage(bmp))
                {
                    Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    //Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, width, height), x, -y, width, height,GraphicsUnit.Pixel);
                    Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, OriginalImage.RawFormat);

                    return ms.GetBuffer();
                }
            }
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}

最佳答案

裁剪看起来并不是从左上角开始,而是从左上角附近开始。在我看来,xy 坐标是错误的。我建议手动插入您自己的 xy 值,直到获得所需的图像,然后找出 x 的原因>y 值与您的预期不同。

我将它与我拥有的一些裁剪代码(并且我知道有效)进行了比较,并且我没有发现您的实际 Crop 方法有任何问题。

关于c# - 图像裁剪无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314001/

相关文章:

c# - 如何从 C# 应用程序调用带有指向 void 的指针的 C++ 函数?

c# - 如何在 ASP.NET Core 的 Swagger 中包含 XML 注释文件

c# - 如何在附加到 DBContext 之前单独通过属性检索 EF 4 代码优先实体的 [Key]?

java - Android图像裁剪正在减小图像的大小

matlab - 在 Matlab 中使用 hgtransform 命令绘制 3D 动画图

Xamarin Android 部署上的 Java、Lang.OutOfMemoryError

image - Bitmap 和 Pixmap 有什么区别?

iphone - 图像未在 iOS 3.2 上显示

c# - c#中的图形面板

c++ - 是否有一种有效的标准算法来栅格包括其内部区域的多边形