How to crop an image using C# with the WebImage class?
I have an application that is written using C#
on the top on ASP.NET MVC 5 framework.
I am trying to use WebImage to crop an image that I have on disk. On the GUI, I am using Jcrop plugin to allow the user to mark the area they want to crop aka keep. As you can see the Jcrop plugin provides me the X1, X2, Y1, and Y2 which identify the location of the final image to keep. Jcrop also provides me with the final height and width of the final image even though they can also be calculated using the following formula W = X2 - X1
; H = Y2 - Y1
Here is my method in which I am cropping a given image and overriding it.
/// <summary>
/// Crop the given image and overrides it with the cropped image
/// </summary>
/// <param name="filename">The full path of the image and the location of the new image</param>
/// <param name="top">Y or Y1</param>
/// <param name="left">X or X1</param>
/// <param name="bottom">Y2</param>
/// <param name="right">X2</param>
/// <returns></returns>
public WebImage CropAndSave(string sourcePath, int top, int left, int bottom, int right)
{
byte imageBytes = File.ReadAllBytes(sourcePath);
var image = new WebImage(imageBytes)
{
FileName = ExtractFileName(sourcePath)
};
WebImage croppedImage = image.Crop(top, left, bottom, right);
croppedImage.Save(sourcePath, "jpg", true);
return croppedImage;
}
However, the cropped image is not what I expect. It is a very small image, not the same one that the user wants to keep.
How can I use WebImage.Crop(...)
to correctly crop the image?
c# jcrop webimage
add a comment |
I have an application that is written using C#
on the top on ASP.NET MVC 5 framework.
I am trying to use WebImage to crop an image that I have on disk. On the GUI, I am using Jcrop plugin to allow the user to mark the area they want to crop aka keep. As you can see the Jcrop plugin provides me the X1, X2, Y1, and Y2 which identify the location of the final image to keep. Jcrop also provides me with the final height and width of the final image even though they can also be calculated using the following formula W = X2 - X1
; H = Y2 - Y1
Here is my method in which I am cropping a given image and overriding it.
/// <summary>
/// Crop the given image and overrides it with the cropped image
/// </summary>
/// <param name="filename">The full path of the image and the location of the new image</param>
/// <param name="top">Y or Y1</param>
/// <param name="left">X or X1</param>
/// <param name="bottom">Y2</param>
/// <param name="right">X2</param>
/// <returns></returns>
public WebImage CropAndSave(string sourcePath, int top, int left, int bottom, int right)
{
byte imageBytes = File.ReadAllBytes(sourcePath);
var image = new WebImage(imageBytes)
{
FileName = ExtractFileName(sourcePath)
};
WebImage croppedImage = image.Crop(top, left, bottom, right);
croppedImage.Save(sourcePath, "jpg", true);
return croppedImage;
}
However, the cropped image is not what I expect. It is a very small image, not the same one that the user wants to keep.
How can I use WebImage.Crop(...)
to correctly crop the image?
c# jcrop webimage
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13
add a comment |
I have an application that is written using C#
on the top on ASP.NET MVC 5 framework.
I am trying to use WebImage to crop an image that I have on disk. On the GUI, I am using Jcrop plugin to allow the user to mark the area they want to crop aka keep. As you can see the Jcrop plugin provides me the X1, X2, Y1, and Y2 which identify the location of the final image to keep. Jcrop also provides me with the final height and width of the final image even though they can also be calculated using the following formula W = X2 - X1
; H = Y2 - Y1
Here is my method in which I am cropping a given image and overriding it.
/// <summary>
/// Crop the given image and overrides it with the cropped image
/// </summary>
/// <param name="filename">The full path of the image and the location of the new image</param>
/// <param name="top">Y or Y1</param>
/// <param name="left">X or X1</param>
/// <param name="bottom">Y2</param>
/// <param name="right">X2</param>
/// <returns></returns>
public WebImage CropAndSave(string sourcePath, int top, int left, int bottom, int right)
{
byte imageBytes = File.ReadAllBytes(sourcePath);
var image = new WebImage(imageBytes)
{
FileName = ExtractFileName(sourcePath)
};
WebImage croppedImage = image.Crop(top, left, bottom, right);
croppedImage.Save(sourcePath, "jpg", true);
return croppedImage;
}
However, the cropped image is not what I expect. It is a very small image, not the same one that the user wants to keep.
How can I use WebImage.Crop(...)
to correctly crop the image?
c# jcrop webimage
I have an application that is written using C#
on the top on ASP.NET MVC 5 framework.
I am trying to use WebImage to crop an image that I have on disk. On the GUI, I am using Jcrop plugin to allow the user to mark the area they want to crop aka keep. As you can see the Jcrop plugin provides me the X1, X2, Y1, and Y2 which identify the location of the final image to keep. Jcrop also provides me with the final height and width of the final image even though they can also be calculated using the following formula W = X2 - X1
; H = Y2 - Y1
Here is my method in which I am cropping a given image and overriding it.
/// <summary>
/// Crop the given image and overrides it with the cropped image
/// </summary>
/// <param name="filename">The full path of the image and the location of the new image</param>
/// <param name="top">Y or Y1</param>
/// <param name="left">X or X1</param>
/// <param name="bottom">Y2</param>
/// <param name="right">X2</param>
/// <returns></returns>
public WebImage CropAndSave(string sourcePath, int top, int left, int bottom, int right)
{
byte imageBytes = File.ReadAllBytes(sourcePath);
var image = new WebImage(imageBytes)
{
FileName = ExtractFileName(sourcePath)
};
WebImage croppedImage = image.Crop(top, left, bottom, right);
croppedImage.Save(sourcePath, "jpg", true);
return croppedImage;
}
However, the cropped image is not what I expect. It is a very small image, not the same one that the user wants to keep.
How can I use WebImage.Crop(...)
to correctly crop the image?
c# jcrop webimage
c# jcrop webimage
edited Nov 19 '18 at 21:06
user3559349
asked Nov 19 '18 at 18:47
Mike AMike A
3,99163880
3,99163880
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13
add a comment |
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13
add a comment |
2 Answers
2
active
oldest
votes
As per the definition of WebImage
Top: the number of pixels to remove from the top.
Left: the number of pixels to remove from the left.
Bottom: the number of pixels to remove from the bottom.
Right: the number of pixels to remove from the right.
You need to provide the number of pixels to remove not number of pixels to keep as you are doing.
Try changing you call to Corp()
to the following
WebImage croppedImage = image.Crop(top, left, image.Height - bottom, image.Width - right);
The image.Height - bottom
will give me the number of pixels to remove from the bottom where image.Width - right
gives you the number of pixel to remove from the right.
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
add a comment |
Reading the definition you provided Crop
is expecting the following;
- Number of pixels to remove from the top Y1
- Number of pixels to remove from the left X1
- Full Height of the original minus Y2
Full Width of the original minus X2
image.Crop(Y1, X1, image.height - Y2, image.width - X2);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380894%2fhow-to-crop-an-image-using-c-sharp-with-the-webimage-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As per the definition of WebImage
Top: the number of pixels to remove from the top.
Left: the number of pixels to remove from the left.
Bottom: the number of pixels to remove from the bottom.
Right: the number of pixels to remove from the right.
You need to provide the number of pixels to remove not number of pixels to keep as you are doing.
Try changing you call to Corp()
to the following
WebImage croppedImage = image.Crop(top, left, image.Height - bottom, image.Width - right);
The image.Height - bottom
will give me the number of pixels to remove from the bottom where image.Width - right
gives you the number of pixel to remove from the right.
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
add a comment |
As per the definition of WebImage
Top: the number of pixels to remove from the top.
Left: the number of pixels to remove from the left.
Bottom: the number of pixels to remove from the bottom.
Right: the number of pixels to remove from the right.
You need to provide the number of pixels to remove not number of pixels to keep as you are doing.
Try changing you call to Corp()
to the following
WebImage croppedImage = image.Crop(top, left, image.Height - bottom, image.Width - right);
The image.Height - bottom
will give me the number of pixels to remove from the bottom where image.Width - right
gives you the number of pixel to remove from the right.
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
add a comment |
As per the definition of WebImage
Top: the number of pixels to remove from the top.
Left: the number of pixels to remove from the left.
Bottom: the number of pixels to remove from the bottom.
Right: the number of pixels to remove from the right.
You need to provide the number of pixels to remove not number of pixels to keep as you are doing.
Try changing you call to Corp()
to the following
WebImage croppedImage = image.Crop(top, left, image.Height - bottom, image.Width - right);
The image.Height - bottom
will give me the number of pixels to remove from the bottom where image.Width - right
gives you the number of pixel to remove from the right.
As per the definition of WebImage
Top: the number of pixels to remove from the top.
Left: the number of pixels to remove from the left.
Bottom: the number of pixels to remove from the bottom.
Right: the number of pixels to remove from the right.
You need to provide the number of pixels to remove not number of pixels to keep as you are doing.
Try changing you call to Corp()
to the following
WebImage croppedImage = image.Crop(top, left, image.Height - bottom, image.Width - right);
The image.Height - bottom
will give me the number of pixels to remove from the bottom where image.Width - right
gives you the number of pixel to remove from the right.
answered Nov 19 '18 at 20:04
Majd AlhayekMajd Alhayek
26413
26413
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
add a comment |
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
That worked! Thank you
– Mike A
Nov 19 '18 at 20:08
add a comment |
Reading the definition you provided Crop
is expecting the following;
- Number of pixels to remove from the top Y1
- Number of pixels to remove from the left X1
- Full Height of the original minus Y2
Full Width of the original minus X2
image.Crop(Y1, X1, image.height - Y2, image.width - X2);
add a comment |
Reading the definition you provided Crop
is expecting the following;
- Number of pixels to remove from the top Y1
- Number of pixels to remove from the left X1
- Full Height of the original minus Y2
Full Width of the original minus X2
image.Crop(Y1, X1, image.height - Y2, image.width - X2);
add a comment |
Reading the definition you provided Crop
is expecting the following;
- Number of pixels to remove from the top Y1
- Number of pixels to remove from the left X1
- Full Height of the original minus Y2
Full Width of the original minus X2
image.Crop(Y1, X1, image.height - Y2, image.width - X2);
Reading the definition you provided Crop
is expecting the following;
- Number of pixels to remove from the top Y1
- Number of pixels to remove from the left X1
- Full Height of the original minus Y2
Full Width of the original minus X2
image.Crop(Y1, X1, image.height - Y2, image.width - X2);
answered Nov 19 '18 at 20:04


Steve0Steve0
1,5182922
1,5182922
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380894%2fhow-to-crop-an-image-using-c-sharp-with-the-webimage-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm betting image.Crop wants the width and height, not the bottom & right positions. Disclaimer, I do not use webimage, but that's fairly industry standard...
– Trey
Nov 19 '18 at 19:24
Where would I provide the width and height? Here is the signature definition for the Crop image docs.microsoft.com/en-us/dotnet/api/…
– Mike A
Nov 19 '18 at 19:26
Looks like two examples below show what I was talking about :-)
– Trey
Nov 19 '18 at 20:13