Critical Developers

Programmers Knowledge Base

Resize image and save without loosing image quality

The code will resize an image to a fixed size image without loosing its quality. Helpful when we want to generate thumbnails.

// Resize image to generate thumbnails
        public static void ResizeAndSaveImage(string filePathName, Image image, int Width, int Height, bool needToFill)
        {

            Bitmap bmpThumb = null;
            try
            {
                int sourceWidth = image.Width;
                int sourceHeight = image.Height;
                int sourceX = 0;
                int sourceY = 0;
                double destX = 0;
                double destY = 0;

                double nScale = 0;
                double nScaleW = 0;
                double nScaleH = 0;

                nScaleW = ((double)Width / (double)sourceWidth);
                nScaleH = ((double)Height / (double)sourceHeight);
                if (!needToFill)
                {
                    nScale = Math.Min(nScaleH, nScaleW);
                }
                else
                {
                    nScale = Math.Max(nScaleH, nScaleW);
                    destY = (Height - sourceHeight * nScale) / 2;
                    destX = (Width - sourceWidth * nScale) / 2;
                }

                if (nScale > 1)
                    nScale = 1;

                int destWidth = (int)Math.Round(sourceWidth * nScale);
                int destHeight = (int)Math.Round(sourceHeight * nScale);

                bmpThumb = new Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));

                using (Graphics graphic = Graphics.FromImage(bmpThumb))
                {
                    graphic.Clear(Color.White);
                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = SmoothingMode.HighQuality;
                    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphic.CompositingQuality = CompositingQuality.HighQuality;

                    Rectangle to = new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
                    Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);

                    graphic.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);
                    image = bmpThumb;
                    image.Save(filePathName, ImageFormat.Jpeg);
                }
            }
            catch
            {
                if (bmpThumb != null) bmpThumb.Dispose();
                throw;
            }
        }


MVC Interview Questions

What is MVC(Model view controller)?  
Can you explain the complete flow of MVC?  
Is MVC suitable for both windows and web application?  
What are the benefits of using MVC?  
Is MVC different from a 3 layered architecture?  
What is the latest version of MVC? What is the difference between each version of MVC? What are routing in MVC?  
Where is the route mapping code written?  
Can we map multiple URL’s to the same action?  
How can we navigate from one view to other view using hyperlink?  
How can we restrict MVC actions to be invoked only by GET or POST?  
How can we maintain session in MVC? What is the difference between tempdata,viewdata and viewbag?  
What are partial views in MVC?  
How did you create partial view and consume the same?  
How can we do validations in MVC? Can we display all errors in one go?  
How can we enable data annotation validation on client side? What is razor in MVC? Why razor when we already had ASPX?  
So which is a better fit Razor or ASPX? How can you do authentication and authorization in MVC?  
How to implement windows authentication for MVC?  
How do you implement forms authentication in MVC?  
How to implement Ajax in MVC?  
What kind of events can be tracked in AJAX? What is the difference between “ActionResult” and “ViewResult”? What are the different types of results in MVC?  
What are “ActionFilters”in MVC? Can we create our custom view engine using MVC?  
How to send result back in JSON format in MVC? What is “WebAPI”? But WCF SOAP also does the same thing, so how does “WebAPI” differ?  
With WCF also you can implement REST, So why "WebAPI"?  
How can we detect that a MVC controller is called by POST or GET?
Breifly explain us what is ASP.Net MVC?
Tell us something about Model, view and Controllers in Asp.Net MVC?
Do you know about the new features in ASP.Net MVC 4 (ASP.Net MVC4)?
How does the 'page lifecycle' of ASP.Net MVC works?
Explain the advantages of ASP.Net MVC over ASP.NET?
What is Separation of Concerns in ASP.NET ASP.Net MVC?
What is Razor View Engine?
What is the meaning of Unobtrusive JavaScript? Explain us by any practical example.
What is the use of View Model in ASP.Net MVC?
What you mean by Routing in ASP.Net MVC?
What are Actions in ASP.Net MVC?
What is Attribute Routing in ASP.Net MVC?
How to enable Attribute Routing?
Explain JSON Binding?
Explain Dependency Resolution?
Explain Bundle.Config in ASP.Net MVC4?
How route table has been created in ASP.NET ASP.Net MVC?
Which are the important namespaces used in ASP.Net MVC?
What is ViewData?
What is the difference between ViewBag and ViewData in ASP.Net MVC?
Explain TempData in ASP.Net MVC?
What are HTML Helpers in ASP.Net MVC?
What are AJAX Helpers in ASP.Net MVC?
What are the options can be configured in AJAX helpers?
What is Layout in ASP.Net MVC?
Explain Sections is ASP.Net MVC?
Can you explain RenderBody and RenderPage in ASP.Net MVC?
What is ViewStart Page in ASP.Net MVC?
Explain the methods used to render the views in ASP.Net MVC?
What are the sub types of ActionResult?
What are Non Action methods in ASP.Net MVC?
How to change the action name in ASP.Net MVC?
What are Code Blocks in Views?
What is the "HelperPage.IsAjax" Property?
How we can call a JavaScript function on the change of a Dropdown List in ASP.Net MVC?
What are Validation Annotations?
Why to use Html.Partial in ASP.Net MVC?
What is Html.RenderPartial?
What is RouteConfig.cs in ASP.Net MVC 4?
What are Scaffold templates in ASP.Net MVC?
Explain the types of Scaffoldings.
Can a view be shared across multiple controllers? If Yes, How we can do that?
What are the components required to create a route in ASP.Net MVC?
Why to use "{resource}.axd/{*pathInfo}" in routing in ASP.Net MVC?
Can we add constraints to the route? If yes, explain how we can do it?
What are the possible Razor view extensions?
What is PartialView in ASP.Net MVC?
How we can add the CSS in ASP.Net MVC?
Can I add ASP.Net MVC Testcases in Visual Studio Express?
What is the use .Glimpse in ASP.Net MVC?
What is the need of Action Filters in ASP.Net MVC?
Mention some action filters which are used regularly in ASP.Net MVC?
How can we determine action invoked from HTTP GET or HTTP POST?
In Server how to check whether model has error or not in ASP.Net MVC?
How to make sure Client Validation is enabled in ASP.Net MVC?
What are Model Binders in ASP.Net MVC?
How we can handle the exception at controller level in ASP.Net MVC?
Does Tempdata hold the data for other request in ASP.Net MVC?
Explain Keep method in Tempdata in ASP.Net MVC?
Explain Peek method in Tempdata in ASP.Net MVC?
What is Area in ASP.Net MVC?
How we can register the Area in ASP.Net MVC?
What are child actions in ASP.Net MVC?
How we can invoke child actions in ASP.Net MVC?
What is Dependency Injection in ASP.Net MVC?
Explain the advantages of Dependency Injection (DI) in ASP.Net MVC?
Explain Test Driven Development (TDD) ?
Explain the tools used for unit testing in ASP.Net MVC?
What is Representational State Transfer (REST) mean?
How to use Jquery Plugins in ASP.Net MVC validation?
How we can multiple submit buttons in ASP.Net MVC?
What are the differences between Partial View and Display Template and Edit Templates in ASP.Net MVC?
Can I set the unlimited length for "maxJsonLength" property in config?
Can I use Razor code in Javascript in ASP.Net MVC?
How can I return string result from Action in ASP.Net MVC?
How to return the JSON from action method in ASP.Net MVC?