我有一个 Azure 函数,想要裁剪字节数组内的图像,然后将其保存到 blob。我不知道如何进行裁剪部分,因为位图在 Azure Functions 中不可用。这是我保存到 blob 的内容:
private async static Task<string> CreateBlob(string name, byte[] data, TraceWriter log)
它的作用很好。另外,我还有要裁剪的图像的 URL,如果有帮助的话。只是不知道如何使用 Azure 函数来实现这一点。有什么想法吗?
编辑
这是原始图像,我在字节数组中。我想从中裁剪另一张尺寸为 - 650x290
int cropStartX = 0;
int cropStartY = 0;
int cropWidth = 650;
int cropHeight = 290;
int multiply = cropWidth * cropHeight;
byte[] croppedImage = new byte[multiply];
for (int j = cropStartX; j < cropStartX + cropWidth; j++)
{
for (int k = cropStartY; k < cropStartY + cropHeight; k++)
{
if ((j + k * cropWidth) >= ((byte[])OriginalPic).Length)
break;
croppedImage[j + k * cropWidth] = ((byte[])OriginalPic)[j + k * cropWidth];
}
}
这是 OriginalPic 的大小 - 字节[49121]
这是我设置后的输出:
croppedImage[j + k * cropWidth] = ((byte[])OriginalPic)[j + k * (cropWidth + 20)];
最佳答案
我不知道这是否是您想要的,但假设您知道图片的尺寸,您只需将其编码到索引中即可:
伪代码
int cropWidth = 100;
int cropHeight = 50;
byte[cropWidth*cropHeight] croppedImage;
for(int i=cropStartX;i<cropStartX+cropWidth;i++)
{
for(int ii = cropStartY;ii<cropStartY+cropHeight;ii++)
{
croppedImage[i + ii*cropWidth] = image[i + ii*mainImageWidth];
}
}
我知道这并不像您实际上可以执行内存复制功能那么快......但会做您需要的事情。
<小时/>更新:在查看了您的更多信息后,我担心此方法对您不起作用,因为您已经压缩了图像数据。基本上 jpeg 以大约 1:10 的大小压缩数据,现在您的图像字节数组只有 49k,这更有意义,因为这几乎正是文件大小,其中包含 1024x434 rgb 图片的 jpeg 压缩信息。长话短说,您不能简单地从该数组中查询像素位置来获取有意义的数据,除非您要在裁剪之前解压缩图像。抱歉,因为我一注意到“字节”应该代表一个 RGB 像素(不匹配)就应该看到这一点。
关于c# - 如何裁剪字节数组图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49803443/