C# 图像裁剪无法正常工作
代码片段应该裁剪图像,但是裁剪的大小是正确的,但是裁剪从左上角开始。这些图像更清楚、更准确地说明了问题C# 图像裁剪无法正常工作,c#,image,graphics,bitmap,memorystream,C#,Image,Graphics,Bitmap,Memorystream,代码片段应该裁剪图像,但是裁剪的大小是正确的,但是裁剪从左上角开始。这些图像更清楚、更准确地说明了问题 /// <summary> /// Handles the Click event of the UploadButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The
/// <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;
}
}
坐标约为: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;
}
}
//
///处理UploadButton控件的单击事件。
///
///事件的来源。
///包含事件数据的实例。
///
受保护的无效上载按钮\u单击(对象发送者,事件参数e)
{
字符串路径=HttpContext.Current.Request.PhysicalApplicationPath+“images\\”;
布尔FileOK=false;
布尔FileSaved=false;
if(Upload.HasFile)
{
会话[“WorkingImage”]=Upload.FileName;
String FileExtension=Path.GetExtension(会话[“WorkingImage”].ToString()).ToLower();
字符串[]allowedExtensions={.png“,.jpeg“,.jpg“,.gif”};
for(int i=0;i
裁剪看起来不像是从左上角开始的,就在左上角附近。在我看来,x
和y
坐标是错误的。我建议您手动插入自己的x
和y
值,直到获得所需的图像,然后找出x
和y
值与预期值不同的原因
/// <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;
}
}
我将它与我拥有的一些裁剪代码进行了比较(我知道这些代码是有效的),我看不出您实际的
Crop
方法有任何问题。只是一个注释:捕获异常只是重新抛出它是绝对没有意义的。让事情变得更糟抛出e
将实际替换原始调用堆栈。@BrianRasmussen true,虽然说实话,我复制并粘贴了,对此并不担心,但感谢您注意到它。很好的观察,我发现它确实不是从左上角开始的。结果显示的图像设置为200*200px,当它的实际尺寸可能更大时。JCrop从img元素中获取命令,但c#代码作用于实际图像。因此,我必须计算img元素与实际图像的比率,然后补偿JCrop值。i、 e.存在各种各样的缩放问题。
/// <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;
}
}