我的任务是将两个图像发布到服务器,将它们组合在一起(一个在另一个上面),然后将生成的图像保存到Azure存储。我有适用的代码,但它只是看起来......错了。 是否有一种不那么难看的方式呢?我讨厌看到所有那些“使用”语句和长缩进。
此代码将签名图像和首字母图像组合在一起,然后将结果保存到Azure存储。
代码:
string initialscontainerPath = "signatures/initailsdata.png";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnection"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(Request.Url.Host.ToLower().Replace(".", "-"));
CloudBlockBlob blockBlob = container.GetBlockBlobReference(initialscontainerPath);
blockBlob.Properties.ContentType = "image/png";
byte[] initialsbytes = Convert.FromBase64String(initialsData);
byte[] signaturebytes = Convert.FromBase64String(signatureData);
using (System.IO.MemoryStream msinitials = new System.IO.MemoryStream(initialsbytes))
{
using (System.IO.MemoryStream mssignature = new System.IO.MemoryStream(signaturebytes))
{
using (System.Drawing.Image bminitials = new System.Drawing.Bitmap(msinitials))
{
using (System.Drawing.Image bmsignature = new System.Drawing.Bitmap(mssignature))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmsignature))
{
g.DrawImage(bminitials, 0, 0);
using (System.IO.MemoryStream savestream = new System.IO.MemoryStream())
{
bmsignature.Save(savestream, System.Drawing.Imaging.ImageFormat.Png);
using (System.IO.MemoryStream uploadstream = new System.IO.MemoryStream(savestream.ToArray()))
{
blockBlob.UploadFromStream(uploadstream);
}
}
}
}
}
}
}
答案 0 :(得分:2)
你有没有看过另一个画一个?只需将图像读入位图对象,然后将较小的图像绘制在另一个上面,有点像:
public Bitmap Combine(Bitmap largeBmp, Bitmap smallBmp) {
Graphics g = Graphics.FromImage(largeBmp);
g.CompositingMode = CompositingMode.SourceOver;
int margin = 5;
int x = largeBmp.Width - smallBmp.Width - margin;
int y = largeBmp.Height - smallBmp.Height - margin;
g.DrawImage(smallBmp, new Point(x, y));
return largeBmp;
}
请注意,x和y是小图像将在较大图像上退出的坐标。
希望有所帮助
答案 1 :(得分:0)
使用刚刚转换为try ... catch ... finaly block并且在最后变量被处理掉。此外,您还可以消除命名空间,并在代码的开头添加更多using
语句。我认为这更具可读性:
string initialscontainerPath = "signatures/initailsdata.png";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("StorageConnection"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(Request.Url.Host.ToLower().Replace(".", "-"));
CloudBlockBlob blockBlob = container.GetBlockBlobReference(initialscontainerPath);
blockBlob.Properties.ContentType = "image/png";
byte[] initialsbytes = Convert.FromBase64String(initialsData);
byte[] signaturebytes = Convert.FromBase64String(signatureData);
CombineBitmaps(initialsbytes, signaturebytes);
CombineBitmaps的位置
private void CombineBitmaps(byte[] initialsbytes, byte[] signaturebytes, CloudBlockBlob blockBlob)
{
MemoryStream msinitials = null;
MemoryStream mssignature = null;
MemoryStream savestream = null;
MemoryStream uploadstream = null;
Bitmap bminitials = null;
Bitmap bmsignature = null;
Graphics g = null;
try
{
msinitials = new MemoryStream(initialsbytes);
mssignature = new MemoryStream(signaturebytes);
bminitials = new Bitmap(msinitials);
bmsignature = new Bitmap(mssignature);
savestream = new MemoryStream();
g = Graphics.FromImage(bmsignature);
g.DrawImage(bminitials, 0, 0);
bmsignature.Save(savestream, ImageFormat.Png);
uploadstream = new MemoryStream(savestream.ToArray());
blockBlob.UploadFromStream(uploadstream);
}
finally
{
DisposeNotNull(msinitials, mssignature, bminitials, bmsignature, g, savestream, uploadstream);
}
}
private void DisposeNotNull(params IDisposable[] objects)
{
foreach(IDisposable obj in objects)
if (obj != null) obj.Dispose();
}
变量名称可以使用一些pascal caseing(或者名称是什么)。