3

ファイルをAzureストレージアカウントにアップロードするために使用する次の機能があります。

ご覧のとおり、サイズ変更などは行われません。

public string UploadToCloud(FileUpload fup, string containerName) { // 接続文字列からストレージ アカウントを取得します。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;
        }

今私がやろうとしているのは、2つをマージしようとすることです...または、少なくとも画像をAzureに保存する前にサイズを変更できる方法を見つけてください。

誰にもアイデアはありますか?

4

2 に答える 2

1

すでに機能していることを願っていますが、今日は機能するソリューションを探していましたが、見つかりません。しかし、ついにやった。

これが私のコードです。誰かに役立つことを願っています:

    /// <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;
    }
于 2015-03-20T23:49:06.423 に答える
0

Windows Phone 8 アプリでサイズ変更操作を行います。イメージできるように、WP8 のランタイムは .NET ランタイム全体に比べてはるかに制限されているため、私と同じ制限がない場合は、これをより効率的に行うための他のオプションがあるかもしれません。

public static void ResizeImageToUpload(Stream imageStream, PhotoResult e)
    {            
        int fixedImageWidthInPixels = 1024;
        int verticalRatio = 1;

        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(e.ChosenPhoto);
        WriteableBitmap writeableBitmap = new WriteableBitmap(bitmap);
        if (bitmap.PixelWidth > fixedImageWidthInPixels)
            verticalRatio = bitmap.PixelWidth / fixedImageWidthInPixels;
        writeableBitmap.SaveJpeg(imageStream, fixedImageWidthInPixels,
            bitmap.PixelHeight / verticalRatio, 0, 80);                                       
    }

幅が 1024 ピクセルより大きい場合、コードは画像のサイズを変更します。この変数verticalRatioは、サイズ変更中に縦横比が失われないように、画像の比率を計算するために使用されます。次に、コードは元の画像の品質の 80% を使用して新しい jpeg をエンコードします。

お役に立てれば

于 2013-12-17T18:33:50.783 に答える