首页 > 解决方案 > 无需下载即可在服务器上调整图像大小

问题描述

我在 asp.net 的服务器上有图像。我想通过复制、调整大小、重命名并最后将它们保存在服务器本身来创建他们的缩略图。对于压缩,我有代码,但如何保存文件。

if (fileUploader.HasFile)
                        {
                            string fileName = Path.GetFileName(fileUploader.PostedFile.FileName);
                            string ext = string.Empty;
                            ext = System.IO.Path.GetExtension(fileUploader.FileName.ToString()).ToLower();
                            fileUploader.PostedFile.SaveAs(Server.MapPath("~/Images_Coach/" + hdnCoachId.Value + "/") + hdnCoachId.Value + ext);
                            int width = Convert.ToInt32(150);
                            int height = Convert.ToInt32(150);
                            Stream inp_Stream = fileUploader.PostedFile.InputStream;
                            using (var image = System.Drawing.Image.FromStream(inp_Stream))
                            {
                                Bitmap myImg = new Bitmap(width, height);
                                Graphics myImgGraph = Graphics.FromImage(myImg);
                                myImgGraph.CompositingQuality = CompositingQuality.HighQuality;
                                myImgGraph.SmoothingMode = SmoothingMode.HighQuality;
                                myImgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                var imgRectangle = new Rectangle(0, 0, width, height);
                                myImgGraph.DrawImage(image, imgRectangle);

                                newFile = hdnCoachId.Value + "_icon" + ext;
                                // Save the file   
                                var path = Path.Combine(Server.MapPath("~/Images_Coach/" + hdnCoachId.Value + "/"), newFile);
                                myImg.Save(path, image.RawFormat);
                            }
                        }  

标签: asp.netimageresize

解决方案


我可以将文件转换为字节数组,然后将其转换为流

 foreach (var fileName in Directory.GetFiles(dirFile))
                    {
                        if (fileName.Contains(dir))
                        {
                            string newFile = string.Empty;
                            //Read the File into a Byte Array.
                            string ext = string.Empty;
                            ext = System.IO.Path.GetExtension(fileName.ToString()).ToLower();
                            byte[] bytes = File.ReadAllBytes(fileName);
                            Stream inp_Stream = new MemoryStream(bytes);
                            int width = Convert.ToInt32(150);
                            int height = Convert.ToInt32(150);
                            using (var image = System.Drawing.Image.FromStream(inp_Stream))
                            {
                                Bitmap myImg = new Bitmap(width, height);
                                Graphics myImgGraph = Graphics.FromImage(myImg);
                                myImgGraph.CompositingQuality = CompositingQuality.HighQuality;
                                myImgGraph.SmoothingMode = SmoothingMode.HighQuality;
                                myImgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                var imgRectangle = new Rectangle(0, 0, width, height);
                                myImgGraph.DrawImage(image, imgRectangle);

                                newFile = dir + "_icon" + ext;
                                // Save the file   
                                var newPath = Path.Combine(Server.MapPath("~/Images_Coach/" + dir + "/"), newFile);
                                myImg.Save(newPath, image.RawFormat);
                            }
                        }
                        //File.Delete
                        // fileName  is the file name
                    }

推荐阅读