gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:12:05 25 4
gpt4 key购买 nike

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

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

公共(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com