c#-4.0 - 在服务器上调整图像大小,然后上传到 azure

标签 c#-4.0 azure-storage

我有以下函数,用于将文件上传到天蓝色存储帐户。

正如您将看到的,它不会调整大小等:

公共(public)字符串UploadToCloud(FileUpload fup,字符串容器名称) { //从连接字符串中检索存储帐户。 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve a reference to a container. 
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

    string newName = "";
    string ext = "";
    CloudBlockBlob blob = null;


    // Create the container if it doesn't already exist.
    container.CreateIfNotExists();

    newName = "";
    ext = Path.GetExtension(fup.FileName);

    newName = string.Concat(Guid.NewGuid(), ext);

    blob = container.GetBlockBlobReference(newName);

    blob.Properties.ContentType = fup.PostedFile.ContentType;

    //S5: Upload the File as ByteArray            
    blob.UploadFromStream(fup.FileContent);

    return newName;


}

然后我就拥有了这个函数,我曾在未托管在 azure 上的网站上使用过该函数:

public string ResizeandSave(FileUpload fileUpload, int width, int height, bool deleteOriginal, string tempPath = @"~\tempimages\", string destPath = @"~\cmsgraphics\")
        {
            fileUpload.SaveAs(Server.MapPath(tempPath) + fileUpload.FileName);

            var fileExt = Path.GetExtension(fileUpload.FileName);
            var newFileName = Guid.NewGuid().ToString() + fileExt;

            var imageUrlRS = Server.MapPath(destPath) + newFileName;

            var i = new ImageResizer.ImageJob(Server.MapPath(tempPath) + fileUpload.FileName, imageUrlRS, new ImageResizer.ResizeSettings(
                            "width=" + width + ";height=" + height + ";format=jpg;quality=80;mode=max"));

            i.CreateParentDirectory = true; //Auto-create the uploads directory.
            i.Build();

            if (deleteOriginal)
            {
                var theFile = new FileInfo(Server.MapPath(tempPath) + fileUpload.FileName);

                if (theFile.Exists)
                {
                    File.Delete(Server.MapPath(tempPath) + fileUpload.FileName);
                }
            }

            return newFileName;
        }

现在我想做的是尝试合并两者......或者至少找出一种能够在将图像存储到天蓝色之前调整图像大小的方法。

大家有什么想法吗?

最佳答案

我希望你已经让它工作了,但我今天正在寻找一些工作解决方案,但我找不到它。但最终我做到了。

这是我的代码,希望对某人有所帮助:

    /// <summary>
    /// Saving file to AzureStorage
    /// </summary>
    /// <param name="containerName">BLOB container name</param>
    /// <param name="MyFile">HttpPostedFile</param>
    public static string SaveFile(string containerName, HttpPostedFile MyFile, bool resize, int newWidth, int newHeight)
    {
        string fileName = string.Empty;

        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExists(BlobContainerPublicAccessType.Container);

            string timestamp = Helper.GetTimestamp() + "_";
            string fileExtension = System.IO.Path.GetExtension(MyFile.FileName).ToLower();
            fileName = timestamp + MyFile.FileName;

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            blockBlob.Properties.ContentType = MimeTypeMap.GetMimeType(fileExtension);

            if (resize)
            {
                Bitmap bitmap = new Bitmap(MyFile.InputStream);

                int oldWidth = bitmap.Width;
                int oldHeight = bitmap.Height;

                GraphicsUnit units = System.Drawing.GraphicsUnit.Pixel;
                RectangleF r = bitmap.GetBounds(ref units);
                Size newSize = new Size();

                float expectedWidth = r.Width;
                float expectedHeight = r.Height;
                float dimesion = r.Width / r.Height;

                if (newWidth < r.Width)
                {
                    expectedWidth= newWidth;
                    expectedHeight = expectedWidth/ dimesion;
                }
                else if (newHeight < r.Height)
                {
                    expectedHeight = newHeight;
                    expectedWidth= dimesion * expectedHeight;
                }
                if (expectedWidth> newWidth)
                {
                    expectedWidth= newWidth;
                    expectedHeight = expectedHeight / expectedWidth;
                }
                else if (nPozadovanaVyska > newHeight)
                {
                    expectedHeight = newHeight;
                    expectedWidth= dimesion* expectedHeight;
                }
                newSize.Width = (int)Math.Round(expectedWidth);
                newSize.Height = (int)Math.Round(expectedHeight);

                Bitmap b = new Bitmap(bitmap, newSize);

                Image img = (Image)b;
                byte[] data = ImageToByte(img);

                blockBlob.UploadFromByteArray(data, 0, data.Length);
            }
            else
            {
                blockBlob.UploadFromStream(MyFile.InputStream);
            }                
        }
        catch
        {
            fileName = string.Empty;
        }

        return fileName;
    }

    /// <summary>
    /// Image to byte
    /// </summary>
    /// <param name="img">Image</param>
    /// <returns>byte array</returns>
    public static byte[] ImageToByte(Image img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();

            byteArray = stream.ToArray();
        }
        return byteArray;
    }

关于c#-4.0 - 在服务器上调整图像大小,然后上传到 azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914155/

相关文章:

c#-4.0 - 时间:2019-03-17 标签:c#genericsnew()constraints with type : (new(T))

entity-framework - EF4 Context.ApplyCurrentValues 不更新当前值

c# - Azure 表存储范围查询,常用条件包含在方法中

c# - 无法加载文件或程序集 'msshrtmi' 或其依赖项之一(Azure 表存储访问)

azure - Azure 上已托管哪些大型(大量数据和使用量)应用程序/网站?

wpf - "Measure"的性能问题

c# - 加载 Matlab 程序集时流氓等待 40 秒......如何诊断原因?

c# - 如何应用 Skip and Take on Include

c# - 如何获取 Azure CloudBlobContainer 的大小

azure - 如何使用 Azure 函数在 Azure Blob 中附加文本文件