How to crop an image using C# with the WebImage class?












2














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



enter image description here



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?










share|improve this question
























  • 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
















2














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



enter image description here



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?










share|improve this question
























  • 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














2












2








2







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



enter image description here



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?










share|improve this question















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



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer





















  • That worked! Thank you
    – Mike A
    Nov 19 '18 at 20:08



















2














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);







share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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.






    share|improve this answer





















    • That worked! Thank you
      – Mike A
      Nov 19 '18 at 20:08
















    2














    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.






    share|improve this answer





















    • That worked! Thank you
      – Mike A
      Nov 19 '18 at 20:08














    2












    2








    2






    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 20:04









    Majd AlhayekMajd Alhayek

    26413




    26413












    • 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




    That worked! Thank you
    – Mike A
    Nov 19 '18 at 20:08













    2














    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);







    share|improve this answer


























      2














      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);







      share|improve this answer
























        2












        2








        2






        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);







        share|improve this answer












        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);








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 20:04









        Steve0Steve0

        1,5182922




        1,5182922






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory