2

コードのスニペットは画像をトリミングすることになっていますが、トリミングのサイズは正しいですが、トリミングは左上隅から始まります。これらの画像は、問題をより明確に示していここますここ

座標はおおよそ:x = 68、y = 28、width = 176、height=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;
    }
}
4

1 に答える 1

2

切り抜きは、左上隅のすぐ近くの左上隅から始まっているようには見えません。私には、xとのy座標が間違っているように見えます。x必要な画像が得られるまで手動で値を挿入し、値が期待と異なるy理由を理解することをお勧めします。xy

Crop私が持っている(そして私が知っている)いくつかのトリミングコードと比較しましたが、実際のメソッドに問題はありません。

于 2012-07-03T16:35:25.207 に答える