Click here to Skip to main content
15,887,746 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Developing an election ballot box app. Pin
samflex28-Apr-16 11:58
samflex28-Apr-16 11:58 
GeneralRe: Developing an election ballot box app. Pin
Richard Deeming29-Apr-16 2:04
mveRichard Deeming29-Apr-16 2:04 
GeneralRe: Developing an election ballot box app (SOLVED). Pin
samflex2-May-16 3:22
samflex2-May-16 3:22 
QuestionUpdate progressbar with wcf webservice Pin
Member 1247474222-Apr-16 1:05
Member 1247474222-Apr-16 1:05 
SuggestionRe: Update progressbar with wcf webservice Pin
Richard Deeming22-Apr-16 1:15
mveRichard Deeming22-Apr-16 1:15 
Questionpls Help me..binding dropdown like Country,State,City..when im selecting any one country it showing me all country's state and city.. Pin
sunil321-Apr-16 0:28
sunil321-Apr-16 0:28 
AnswerRe: pls Help me..binding dropdown like Country,State,City..when im selecting any one country it showing me all country's state and city.. Pin
John C Rayan21-Apr-16 0:55
professionalJohn C Rayan21-Apr-16 0:55 
Questionc# Image Processing from byte[] to image, centering the image on a square canvas Pin
jkirkerx20-Apr-16 13:47
professionaljkirkerx20-Apr-16 13:47 
This is for my new program written in MVC, in which I'm using lots avatars to represent things like users and items.

In my old program code in VB, I picked up the image from the disk drive, make a new canvas and wrote the image on the new canvas centering it back to the drive.

In this program, I got slick and took the bytes[] from file upload, and then I feed them into this function that returns the image back to me in various formats such as bytes[]. The program works, but I just can't get the new resized image to center on the canvas, it always starts on the left edge. The height is correct, but I get a black strip on the right side because it's smaller.

I'm not looking for someone to write code for me, just take a look at the function and perhaps you can see where I blew it and made a mistake. Perhaps its the order of graphic objects, maybe I have it backwards.
C#
<pre lang="text">    Your codeblocks should be of the form

    ```C#
/// avatar_resize
/// resizes an image that is a byte[] and return an image model
/// 
/// <param name="pName">Name of the Image</param>
/// <param name="pBytes">Byte Array of the image uploaded to the server</param>
/// <returns>model_image_object, with new resized byte array</returns>
public static model_image_object avatar_resize(string pName, byte[] pBytes)
{
    // Target Width / Height
    // Make a square image
    const int t_Width = 250;
    const int t_Height = 250;
    int cHeight = 0;
    int cWidth = 0;

    // Create the return Image Object
    model_image_object pResult = new model_image_object();
    MemoryStream stream_out = new MemoryStream();
    using (Stream stream_in = new MemoryStream(pBytes))
    {
        // Create an Image so we can get the image parameters
        Image image_in = Image.FromStream(stream_in);

        // Write this to the drive and visually inspect it, is it correct?
        //image_in.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/smtp/image_in.png"));

        // Figure out what we need to do to resize this image
        int oWidth = image_in.Width;
        int oHeight = image_in.Height;
        Color bgColor = Color.FromArgb(255, 255, 255);

        // Create the canvas for the new resized image
        using (Bitmap bitmapObj = new Bitmap(t_Width, t_Height, PixelFormat.Format24bppRgb))
        {
            // Create the new graphics, that will overlay the Bitmap Canvas
            using (Graphics graphicObj = Graphics.FromImage(bitmapObj))
            {
                graphicObj.Clear(Color.Transparent);
                graphicObj.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                graphicObj.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                // Calculate the new Height and Width of the Image
                if (oWidth == oHeight)
                {
                    cWidth = 250;
                    cHeight = 250;
                }
                else if (oWidth > t_Width)
                {
                    cWidth = t_Width;
                    cHeight = (int)Math.Round((oHeight / (float)oWidth) * t_Width);
                }
                else if (oWidth < t_Width)
                {
                    cWidth = (int)Math.Round((oWidth / (float)oHeight) * t_Height);
                    cHeight = (int)Math.Round((oHeight / (float)oWidth) * cWidth);    

                }
                else if (oHeight > t_Height)
                {
                    cHeight = t_Height;
                    cWidth = (int)Math.Round((oWidth / (float)oHeight) * t_Height);
                }

                // Calculate how to overlay the width of the image centered                        
                int x = (cWidth - t_Width) / 2;
                int y = (cHeight - t_Height) / 2;    

                // Create the new Image Object
                graphicObj.DrawImage(image_in, x, y, t_Width, t_Height);

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

            // Now write the image_out to test it and make sure it's correct, right size and placement            
            //bitmapObj.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/avatars/products/bitmap_out.png"));

            // Create a new MemoryStream to Save the Image to
            bitmapObj.Save(stream_out, ImageFormat.Png);
        }
    }

    // Program the pResult
    pResult.Data = stream_out.ToArray();
    pResult.Name = pName;
    pResult.Type = "image/png";
    pResult.Size = new model_image_type_size();
    pResult.Size.Height = cHeight;
    pResult.Size.Width = cWidth;

    return pResult;
}
    ```


public class model_image_object
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public byte[] Data { get; set; }
        public model_image_type_size Size { get; set; }
    }

AnswerRe: c# Image Processing from byte[] to image, centering the image on a square canvas Pin
Richard Deeming21-Apr-16 2:40
mveRichard Deeming21-Apr-16 2:40 
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 
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 

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.