0

Is it possible to square and export a JPEG image like this in .NET (without 3rd party tools)?

The method is to extend the canvas (as a white matte) while keeping the image centered and extending the short dimension of the canvas i.e.

if width is 300 and height is 480 then extend the canvas width to be 480.

ps = new Photoshop.Application();
ps.Preferences.RulerUnits = PsUnits.psPixels;
Console.WriteLine(f.FullName);
ps.Open(f.FullName);

// unlock background to allow canvas resizing
ps.ActiveDocument.ActiveLayer.name = "From Background";
ps.ActiveDocument.ChangeMode(PsChangeMode.psConvertToRGB);

//  resize canvas to square
double width = ps.ActiveDocument.Width;
double height = ps.ActiveDocument.Height;
if (width > height)
{
    ps.ActiveDocument.ResizeCanvas(width, width, 
        PsAnchorPosition.psMiddleCenter);
}
else if (width < height)
{
    ps.ActiveDocument.ResizeCanvas(height, height, 
        PsAnchorPosition.psMiddleCenter);
}

in this before/after i used a gray background in the after image so it is easy to see, but I will use a white matte in prodcution.

BEFORE

AFTER

ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • Putting the code in a GIST means that when the GIST is gone, the question becomes useless to people. From what I can tell you are seeking to crop an image to square around its centre, so maybe just explaining what you want to do in the question would be more use anyway. Have you searched MSDN? http://msdn.microsoft.com/en-us/library/7wt4bf7h(v=vs.110).aspx – glenatron May 27 '14 at 13:04
  • You still need to [edit] and *ask a question*. It's not clear what you're wanting. Try adding image examples, as well. –  May 27 '14 at 13:04
  • Yes, it is always better to add code inline. btw I don't understand your question. What do you meany by square an image? To resize an image [refer here](http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) – Sriram Sakthivel May 27 '14 at 13:05
  • means: make the image dimensions square by extending the narrow dimension of the canvas. If width is 30 and height is 90 then extend the canvas width to be 90px. but if height is 45 and width is 150 then extend the height of the canvas to be 150 (with image in center). – ChatGPT May 27 '14 at 14:13
  • @will question is in title. I'll move to body of post. – ChatGPT May 27 '14 at 14:19
  • @glenatron i clarified the question. I thought the script was fairly readable in English but guess not! There is no crop. The canvas is extended instead. – ChatGPT May 27 '14 at 14:22
  • That is much clearer. I believe the answer to your question is "yes," but I suspect it would be helpful for you to have more detail than that. It's a while since I have worked with .net's graphics stuff, so I probably wouldn't be much help, but basically you will want to create Canvas ( or Graphics or whatever it is called ) of the right size, flood fill it with background then write the image onto it in the right position. – glenatron May 27 '14 at 15:00

0 Answers0