Click here to Skip to main content
15,888,984 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: c# Image Processing from byte[] to image, centering the image on a square canvas [solved] Pin
jkirkerx21-Apr-16 9:09
professionaljkirkerx21-Apr-16 9:09 
Well I've been working on this all morning. And your right, it was my calculation and I did get the Graphics and Bitmap objects right.

So after hours of using my calculator and confirming formulas, I had nothing. Then I decided to search for the same formula but written better and came across this. I looked at it and deemed it to be highly optimized already.

Well at least I got the thought right about canvas, ratios, positions, for the code just slid right into my code.

Thanks Richard, it was head scratcher. But I went with this instead.

On the positive side, I must admit that this function is pretty slick now that it works correctly.
Working with images as byte arrays is pretty cool for storing them in the database and writing them as files for HTTP calls.

On the negative side, working with numbers in c# is much different that VB. C# doesn't divide integers, didn't know that.
graphicObj.Clear(Color.Transparent);
graphicObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
graphicObj.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

// Figure out the ratio
double ratioX = (double)canvasWidth / (double)originalWidth;
double ratioY = (double)canvasHeight / (double)originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;

// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);

// Now calculate the X,Y position of the upper-left corner 
// (one of these will always be zero)
int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);

// Create the new Image Object
graphicObj.Clear(Color.White);
graphicObj.DrawImage(image_in, posX, posY, newWidth, newHeight);

// Save the Graphic Object
graphicObj.Save();

credit goes to
User egrunin - Stack Overflow[^]
QuestionPls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
sunil319-Apr-16 21:37
sunil319-Apr-16 21:37 
AnswerRe: Pls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
Nathan Minier20-Apr-16 1:53
professionalNathan Minier20-Apr-16 1:53 
GeneralRe: Pls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
sunil321-Apr-16 0:26
sunil321-Apr-16 0:26 
AnswerRe: Pls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
John C Rayan20-Apr-16 2:38
professionalJohn C Rayan20-Apr-16 2:38 
GeneralRe: Pls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
sunil321-Apr-16 0:25
sunil321-Apr-16 0:25 
GeneralRe: Pls help getting error..The name 'ObjectFactory' does not exist in the current context Pin
John C Rayan21-Apr-16 0:32
professionalJohn C Rayan21-Apr-16 0:32 
QuestionFreezing Heading of one DataGrid with data from different datasets Pin
Member 1247018419-Apr-16 20:34
Member 1247018419-Apr-16 20:34 
QuestionHow to use HandleUnknownAction Pin
nasirs516-Apr-16 23:13
nasirs516-Apr-16 23:13 
AnswerRe: How to use HandleUnknownAction Pin
John C Rayan17-Apr-16 23:24
professionalJohn C Rayan17-Apr-16 23:24 
QuestionMVC asp.net 4.0 "using templates" Pin
Member 1241562115-Apr-16 1:52
Member 1241562115-Apr-16 1:52 
AnswerRe: MVC asp.net 4.0 "using templates" Pin
John C Rayan15-Apr-16 2:00
professionalJohn C Rayan15-Apr-16 2:00 
AnswerRe: MVC asp.net 4.0 "using templates" Pin
John C Rayan15-Jun-16 1:28
professionalJohn C Rayan15-Jun-16 1:28 
Questionworking in MVC4 ..Getting Error pls help Pin
sunil314-Apr-16 19:35
sunil314-Apr-16 19:35 
AnswerRe: working in MVC4 ..Getting Error pls help Pin
F-ES Sitecore15-Apr-16 0:22
professionalF-ES Sitecore15-Apr-16 0:22 
AnswerRe: working in MVC4 ..Getting Error pls help Pin
John C Rayan15-Apr-16 0:24
professionalJohn C Rayan15-Apr-16 0:24 
Questionpartial views connection of index view Pin
Member 1243381313-Apr-16 21:34
Member 1243381313-Apr-16 21:34 
RantREPOST Pin
Richard Deeming14-Apr-16 1:53
mveRichard Deeming14-Apr-16 1:53 
AnswerRe: partial views connection of index view Pin
F-ES Sitecore14-Apr-16 1:57
professionalF-ES Sitecore14-Apr-16 1:57 
Questiontext Pin
Member 1205765613-Apr-16 19:28
Member 1205765613-Apr-16 19:28 
AnswerRe: text Pin
Richard MacCutchan13-Apr-16 20:47
mveRichard MacCutchan13-Apr-16 20:47 
AnswerRe: text Pin
aarif moh shaikh14-Apr-16 18:43
professionalaarif moh shaikh14-Apr-16 18:43 
QuestionAbout AutoMapper In Asp.net MVC4 Pin
sunil312-Apr-16 20:48
sunil312-Apr-16 20:48 
AnswerRe: About AutoMapper In Asp.net MVC4 Pin
Richard MacCutchan12-Apr-16 21:33
mveRichard MacCutchan12-Apr-16 21:33 
GeneralRe: About AutoMapper In Asp.net MVC4 Pin
sunil312-Apr-16 23:38
sunil312-Apr-16 23:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.