Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

How to determine the image size: image width, image height from an image in Asp.Net Core?

02.05.2018 (👁11033)


I need from the upload photos the current image size as width and height. How can I get these from the file upload?



//< get Size >

System.Drawing.Image image= System.Drawing.Image.FromFile(new_FullFilename_on_Server);

int intWidth = image.Width;

int intHeight = image.Height;

image.Dispose();

//</ get Size >



System:

Asp.Net Core 2 MVC



 


You can evaluate the following values from the System.Drawing.Bitmap at runtime like in the debugger

 

?

Name

Value

Type

image

{System.Drawing.Bitmap}

System.Drawing.Image {System.Drawing.Bitmap}

Flags

77840

int

FrameDimensionsList

{System.Guid[1]}

System.Guid[]

Height

2160

int

HorizontalResolution

72

float

Palette

{System.Drawing.Imaging.ColorPalette}

System.Drawing.Imaging.ColorPalette

PhysicalDimension

{{Width=3840, Height=2160}}

System.Drawing.SizeF

PixelFormat

Format24bppRgb

System.Drawing.Imaging.PixelFormat

PropertyIdList

{int[59]}

int[]

PropertyItems

{System.Drawing.Imaging.PropertyItem[59]}

System.Drawing.Imaging.PropertyItem[]

RawFormat

{[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]}

System.Drawing.Imaging.ImageFormat

Size

{{Width=3840, Height=2160}}

System.Drawing.Size

Tag

null

object

VerticalResolution

72

float

Width

3840

int


 


 


example


Image upload at https://Readdy.Net/Notes/Edit/120




 


Reference Code in C #


Complete file upload code


        [HttpPost]

        [Authorize]

        [RequestSizeLimit(1000000000)]  //1000000000=1000.000.000=1GB

        public async Task<IActionResult> Edit_Upload_Images()

        {

            // -------------< Edit_Upload_Images() > -------------

            //string sFiles_uploaded = "";

            List<string> response_list_of_Filenames = new List<string>();

 

            //< get ID and SubNr >

            //*from Querysting like Notes/Edit_Upload_Images?IDNote=11&ImageNr=1

            string sIDNote = Request.Query["IDNote"];

            long IDNote = System.Convert.ToInt64(sIDNote);

            //</ get ID and SubNr >

 

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDCurrent_User = await UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--

 

            //< get_database >

            NoteModel note;

            if (IDNote == 0)

            {

                note = _dbContext.tbl_Notes.SingleOrDefault(n => n.IDUser == IDCurrent_User && n.IsDraft == true);

                if (note == null)

                {

                    note = new NoteModel();

                    note.IsDraft = true;

                    note.DtCreated = DateTime.Now;

                    note.IDUser = IDCurrent_User;

                    note.Title = "#images";

                    note.sumImages = 0;

                    note.DtEdit = DateTime.Now;

 

                    //< save >

                    await _dbContext.tbl_Notes.AddAsync(note); //IDNote: 0->-99999

                    await _dbContext.SaveChangesAsync();       //IDNote: -99999->16

                    //</ save >

 

                    //< new ID >

                    IDNote = note.IDNote;

                    //Console.WriteLine("new IDNote=" + note.IDNote);

                    //</ new ID >

                }

                else

                {

                    //< last draft >

                    IDNote = note.IDNote;

                    //</ last draft >

                }

            }

            else

            {

                note = _dbContext.tbl_Notes.SingleOrDefault(n => n.IDNote == IDNote);

            }

 

            if (note == null) { return Json("NoMatch for IDNote=" + IDNote); }

            //</ get_database >

 

            //< add_response >

            response_list_of_Filenames.Add("IDNote=" + IDNote);

            //</ add_response >

 

 

            //< check Owner >

            long IDOwner = note.IDUser;

            if (IDOwner != IDCurrent_User) { return Content("You are not the Owner"); }

            //</ check Owner >

 

 

 

 

            //< init >

            //long uploaded_size = 0;

            string path_for_Uploaded_Files = _hostingEnvironment.WebRootPath + "\\User_Files\\Notes\\Images";

            //</ init >

 

            //< get form_files >

            //IFormFile uploaded_File

            var uploaded_files = Request.Form.Files;

            //</ get form_files >

 

            //--< get last Image >--

            //*get User Summary Table

            Note_Image_Model imageData = new Note_Image_Model();

 

            //< last_Image >

            //var lastImage = _dbContext.tbl_Notes_Images.LastOrDefault(img => img.IDNote == IDNote);

            //last Image

            Note_Image_Model lastImage = await _dbContext.tbl_Notes_Images.Where(img => img.IDNote == IDNote).OrderBy(img => img.ImageNr).LastOrDefaultAsync();

            int last_ImageNr = -1;

            if (lastImage != null)

            {

                last_ImageNr = (int)lastImage.ImageNr;

            }

            //</ last_Image >

            //------< @Loop: Uploaded Files >------           

            foreach (var uploaded_file in uploaded_files)

            {

                //----< Uploaded File >----

                try

                {

                    if (uploaded_file.ContentType.IndexOf("image") == 0)   //*image/jpeg

                    {

                        //------< Is_TypeImage >------

                        string uploaded_Filename = uploaded_file.FileName;

                        int posExtension = uploaded_Filename.LastIndexOf(".");

                        string sExtension = uploaded_Filename.Substring(posExtension + 1);

                        sExtension = sExtension.ToLower();

 

                        if (sExtension == "jpg")

                        {

                            //----< IsNot_Jpg >----

 

                            //< convert >

 

                            //</ convert >

                            //----</ IsNot_Jpg >----

                        }

 

                        if (sExtension=="jpg")

                        {

                            //----< Is_jpg >----

 

 

 

 

 

 

                            //--< New Note_Image >--

                            last_ImageNr++;

                            Note_Image_Model newImageData = new Note_Image_Model();

                            newImageData.IDNote = IDNote;

                            newImageData.dtCreate = DateTime.Now;

                            newImageData.ImageNr = last_ImageNr;

                            

                            //< save ImageData >

                            await _dbContext.tbl_Notes_Images.AddAsync(newImageData);

                            await _dbContext.SaveChangesAsync();

                            //</ save ImageData >

 

                            //< add_response >

                            response_list_of_Filenames.Add("Image_" + IDNote + "_" + last_ImageNr);

                            //</ add_response >

 

                            //< Filename >

                            string image_webname = "Image_" + IDNote + "_" + last_ImageNr + ".jpg";

                            string new_FullFilename_on_Server = path_for_Uploaded_Files + "\\" + image_webname;

                            //</ Filename >

 

 

                            //< Copy File to Target >

                            using (FileStream stream = new FileStream(new_FullFilename_on_Server, FileMode.Create))

                            {

                                await uploaded_file.CopyToAsync(stream);

                            }

                            //</ Copy File to Target >

 

                            //< get Size >

                            System.Drawing.Image image= System.Drawing.Image.FromFile(new_FullFilename_on_Server);

                            int intWidth = image.Width;

                            int intHeight = image.Height;

                            image.Dispose();

                            //</ get Size >

 

                            //string test = image.PropertyItems[0].ToString();

 

 

                            //< resize >

                            Image_Methods.Image_resize_to_Width(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_mini.jpg", 200);

                            Image_Methods.Image_resize_to_Width(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_pad.jpg", 400);

                            Image_Methods.Image_resize_Length(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_blog.jpg", 800);

                            Image_Methods.Image_resize_Length(new_FullFilename_on_Server, new_FullFilename_on_Server, 1920);

                            //</ resize >

                            //----</ Is_jpg >----

                        }

                        //------</ Is_TypeImage >------

                    }

                }

                catch (System.Exception) //ex

                {

                    //< error >

                    //  response_list_of_Filenames.Add("Error:" + ex.Message);

                    //</ error >

                }

                //----</ Uploaded File >----

            }

            //------</ @Loop: Uploaded Files >------

 

            //----< Save Data >----

            //--< data >--

            note.sumImages = last_ImageNr + 1;

            note.DtEdit = DateTime.Now;

            //--</ data >--

 

            try

            {

                //todo: _dbContext.Update(note);

                await _dbContext.SaveChangesAsync();

            }

            catch (DbUpdateConcurrencyException)

            {

                return Content("Update Error");

            }

            //----</ Save Data >----

 

            //< response >

            return Json(response_list_of_Filenames);

            //</ response >

            // -------------</ Edit_Upload_Images() > -------------

        }