Critical Developers

Programmers Knowledge Base

Check if any input element has a value in a div/form using jQuery

Check if any input element has a value in a div/form using jQuery

function fnCheckInputHasValue()
{
var isvalpresent = false;
                        $("div#mydiv").find(":input").each(function () {
                            var type = $(this).attr("type");
                            //alert(type);
                            var $this = $(this);
                            if ($this.is("input")) {
                                if (type == "text") {
                                    var val = $(this).val().trim();
                                    if (val != "") {
                                        isvalpresent = true;
                                    }
                                }
                                //
                                if (type == "checkbox") {
                                    if ($(this).is(":checked")) {
                                        isvalpresent = true;
                                    }
                                }
                                //
                                if (type == "radio") {
                                    if ($(this).is(":checked")) {
                                        isvalpresent = true;
                                    }
                                }
                            }
                            //
                            if ($this.is("select")) {
                                var val = $(this).val().trim();
                                if (val != "" && val != "Select") {
                                    isvalpresent = true;
                                }
                            }
                            //
                            if ($this.is("textarea")) {
                                var val = $(this).val().trim();
                                if (val != "") {
                                    isvalpresent = true;
                                }
                            }
                        });

                        if (isvalpresent == false) {
                            // code
                        }

}

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;
            }
        }