c#中的图像处理 [英] image manipulation in c#
本文介绍了c#中的图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个项目中工作,我已经进行了图像处理工作,当图像上传到我的软件时,它必须要求新的图像高度,宽度以及它的大小(以kb为单位)。当我给出这个数据时,软件转换图像并制作具有给定高度,宽度和尺寸的新图像(尺寸小于给定尺寸)。
i必须设置三图像属性。
1.身高
2.宽度
3.尺寸
I work on one project in which i have do image manipulation means when image upload to my software it have to ask for new image height,width and as well as it size in kb . when i gave this data that software convert image and make new image with given height,width and it size (size is less than given size).
i have to set three image attributes.
1. height
2. Width
3. It size
public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
imgPhoto.Dispose();
return bmPhoto;
}
推荐答案
最好使用重新取样。为此,您可以链接到OpenCV(更准确地说是 Emgu CV [ ^ ])例如: http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html [ ^ ]
You better use resampling. And for that, you can link to OpenCV (more precisely Emgu CV[^]) for example: http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html[^]
这篇关于c#中的图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文