コード例 #1
0
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            var          dirAvatarOriginal = AppEnv.UploadImagesOriginalDir;
            const string dirAvatarNormal   = AppEnv.UploadImagesNormalDir;
            const string dirAvatarThumb    = AppEnv.UploadImagesThumbDir;
            var          imgWidth          = 0;
            var          imgHeight         = 0;
            double       filesize          = 0;

            string fileAvatarPath = "", newfileAvatarName = "";
            var    info = new ImageInfo();

            var imageCount = ContentImageDB.GetQuantityImageOfContent(_contentId);

            if (txtFile.PostedFile.FileName.Length > 0)
            {
                //string fileAvatarName = Path.GetFileNameWithoutExtension(txtFile.PostedFile.FileName).Replace(" ", "_").Replace("#", "_");
                newfileAvatarName = "image_" + _contentId + "_" + imageCount + txtFile.PostedFile.FileName.Substring(txtFile.PostedFile.FileName.LastIndexOf('.'));

                var file4 = new FileInfo(Server.MapPath(AppEnv.UploadImagesNormalDir) + newfileAvatarName);
                if (file4.Exists)
                {
                    newfileAvatarName = "image_" + _contentId + "_" + imageCount + "_" + DateTime.Now.ToString("mmss") + txtFile.PostedFile.FileName.Substring(txtFile.PostedFile.FileName.LastIndexOf('.'));
                }
                txtFile.PostedFile.SaveAs(Server.MapPath(dirAvatarOriginal) + newfileAvatarName);
                double filesizeb = txtFile.PostedFile.ContentLength;
                filesize = filesizeb / 1024;

                //thumbnail creation starts
                //                try
                //                {
                // dinh dang width height mac dinh de scale
                int    smallWidth   = ConvertUtility.ToInt32(AppEnv.ThumbWidth);;
                int    smallHeight  = ConvertUtility.ToInt32(AppEnv.ThumbHeight);
                int    normalWidth  = ConvertUtility.ToInt32(AppEnv.NormalWidth);
                int    normalHeight = ConvertUtility.ToInt32(AppEnv.NormalHeight);
                double scalesmall   = 0;
                double scalenormal  = 0;

                string imageUrl = Server.MapPath(dirAvatarOriginal + newfileAvatarName); // xac dinh anh chuan bi thumbnail

                Bitmap InputBitmap = new Bitmap(imageUrl);                               // tao anh bitmap

                // xac dinh % de resize
                imgWidth  = InputBitmap.Width;
                imgHeight = InputBitmap.Height;
                if (smallHeight == 0)//InputBitmap.Height < InputBitmap.Width)
                {
                    scalesmall  = ((double)smallWidth) / InputBitmap.Width;
                    scalenormal = ((double)normalWidth) / InputBitmap.Width;
                }

                int newSmallWidth   = 0;
                int newSmallHeight  = 0;
                int newNormalWidth  = 0;
                int newNormalHeight = 0;
                if (smallHeight == 0)
                {
                    newSmallWidth  = (int)(scalesmall * InputBitmap.Width);
                    newSmallHeight = (int)(scalesmall * InputBitmap.Height);

                    if (normalWidth < imgWidth)
                    {
                        newNormalWidth  = (int)(scalenormal * InputBitmap.Width);
                        newNormalHeight = (int)(scalenormal * InputBitmap.Height);
                    }
                    else
                    {
                        newNormalWidth  = imgWidth;
                        newNormalHeight = imgHeight;
                    }
                }
                else
                {
                    newSmallWidth   = smallWidth;
                    newSmallHeight  = smallHeight;
                    newNormalWidth  = normalWidth;
                    newNormalHeight = normalHeight;
                }

                Bitmap OutputBitmapSmall  = new Bitmap(InputBitmap, newSmallWidth, newSmallHeight);   // tao anh bitmap voi size small moi
                Bitmap OutputBitmapNormal = new Bitmap(InputBitmap, newNormalWidth, newNormalHeight); // tao anh bitmap voi size normal moi

                // xac dinh mime type
                //Response.Clear();
                //Response.ContentType="image/Jpeg";

                //moi
                ImageCodecInfo[]  Info   = ImageCodecInfo.GetImageEncoders();
                EncoderParameters Params = new EncoderParameters(1);
                Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                //Response.ContentType = Info[1].MimeType;
                //thumbnail.Save(Response.OutputStream,Info[1],Params);
                InputBitmap.Dispose();

                OutputBitmapSmall.Save(Server.MapPath(dirAvatarThumb) + newfileAvatarName, Info[1], Params);
                OutputBitmapNormal.Save(Server.MapPath(dirAvatarNormal) + newfileAvatarName, Info[1], Params);

                // thuc hien
                OutputBitmapSmall.Dispose();
                OutputBitmapNormal.Dispose();

                //				}
                //				catch(Exception ex)
                //				{
                //					Response.Write("An error occurred - " + ex.ToString());
                //				}

                //MultimediaUtility.SetThumbnail(Server.MapPath(dirLarge + newfileoutsidename), Server.MapPath(dirThumb), ConvertUtility.ToInt32(Constants.ThumbWidth), ConvertUtility.ToInt32(Constants.ThumbHeight));
                //ImageDB.CreateThumbnail(dirThumb, dirLarge + newfileoutsidename);


                fileAvatarPath = dirAvatarThumb + newfileAvatarName;
                //fileAvatarPath = dirAvatarOriginal + newfileAvatarName;

                //MultimediaUtility.SetAvatarThumbnail(Server.MapPath(fileAvatarPath), 250, 0);
            }
            else
            {
                fileAvatarPath = "";
            }


            if (fileAvatarPath.Length > 0)
            {
                if (txtName.Text.Length > 0)
                {
                    info.Image_Name = ConvertUtility.ToString(txtName.Text);
                }
                else
                {
                    info.Image_Name = newfileAvatarName;
                }
                info.Image_Description = txtTeaser.Text;
                info.Image_File        = fileAvatarPath;
                info.Image_CreateDate  = DateTime.Now;
                info.Image_FileSize    = ConvertUtility.ToDouble(filesize.ToString("#0.00"));
                info.Image_Width       = imgWidth;
                info.Image_Height      = imgHeight;
                info.Image_View        = 0;
                info.User_ID           = ConvertUtility.ToInt32(ckid);
                info.Image_Visible     = chkVisible.Checked;

                try
                {
                    var imgId = ImageDB.Insert(info);

                    var contentImgInfo = new ContentImageInfo();
                    contentImgInfo.Content_ID = _contentId;
                    contentImgInfo.Image_ID   = imgId;
                    if (imageCount == 0)
                    {
                        contentImgInfo.IsCover = true;
                    }
                    else
                    {
                        contentImgInfo.IsCover = false;
                    }
                    contentImgInfo.Priority = imageCount;

                    ContentImageDB.Insert(contentImgInfo);

                    Response.Redirect(Request.RawUrl + "#idTab2");

                    lblStatusUpdate.Text = lblStatusUpdate2.Text = MiscUtility.UPDATE_SUCCESS;
                }
                catch
                {
                    lblStatusUpdate.Text = lblStatusUpdate2.Text = MiscUtility.UPDATE_ERROR;
                }
            }
        }