我已经存储到SD卡中的图像。 我想将图像划分为16等份。 如何使用位图办呢?
解决方案 公共类CropImageManipulator
{
公共CropImageManipulator()
{
}
私人字符串_fileNameWithoutExtension;
私人字符串_fileExtension;
私人字符串_fileDirectory;
公共无效裁剪(字符串inputImgPath,诠释cropWidth,INT cropHeight)
{
this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);
//装载分割图像
图片inputImg = Image.FromFile(inputImgPath);
INT imgWidth = inputImg.Width;
INT imgHeight = inputImg.Height;
//划分多少小块
INT widthCount =(int)的Math.Ceiling((imgWidth * 1.00)/(cropWidth * 1.00));
INT heightCount =(int)的Math.Ceiling((imgHeight * 1.00)/(cropHeight * 1.00));
ArrayList的areaList =新的ArrayList();
INT I = 0;
对于(INT IHEIGHT = 0; IHEIGHT< heightCount; IHEIGHT ++)
{
对于(INT iWidth = 0; iWidth< widthCount; iWidth ++)
{
INT pointX = iWidth * cropWidth;
INT尖尖= IHEIGHT * cropHeight;
INT areaWidth =((pointX + cropWidth)> imgWidth)? (imgWidth - pointX):cropWidth;
INT areaHeight =((尖尖+ cropHeight)> imgHeight)? (imgHeight - 尖):cropHeight;
字符串s =的String.Format({0} {1} {2} {3},pointX,尖尖的,areaWidth,areaHeight);
矩形RECT =新的Rectangle(pointX,尖尖的,areaWidth,areaHeight);
areaList.Add(RECT);
我++;
}
}
对于(INT ILOOP = 0; ILOOP< areaList.Count; ILOOP ++)
{
矩形RECT =(矩形)areaList [ILOOP]
字符串文件名= this._fileDirectory +\\+ this._fileNameWithoutExtension +_+ iLoop.ToString()+ this._fileExtension;
位图newBmp =新位图(rect.Width,rect.Height,PixelFormat.Format24bppRgb);
图形newBmpGraphics = Graphics.FromImage(newBmp);
newBmpGraphics.DrawImage(inputImg,新的Rectangle(0,0,rect.Width,rect.Height),矩形,GraphicsUnit.Pixel);
newBmpGraphics.Save();
开关(this._fileExtension.ToLower())
{
案.JPG:
案.JPEG:
newBmp.Save(文件名,ImageFormat.Jpeg);
打破;
案GIF:
newBmp.Save(文件名,ImageFormat.Gif);
打破;
}
}
inputImg.Dispose();
}
}
I have images stored into sd card. I want to divide the image into sixteen equal parts. How to do it using bitmap?
解决方案public class CropImageManipulator
{
public CropImageManipulator()
{
}
private string _fileNameWithoutExtension;
private string _fileExtension;
private string _fileDirectory;
public void Cropping(string inputImgPath, int cropWidth, int cropHeight)
{
this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);
//Load the image divided
Image inputImg = Image.FromFile(inputImgPath);
int imgWidth = inputImg.Width;
int imgHeight = inputImg.Height;
//Divide how many small blocks
int widthCount = (int)Math.Ceiling((imgWidth * 1.00) / (cropWidth * 1.00));
int heightCount = (int)Math.Ceiling((imgHeight * 1.00) / (cropHeight * 1.00));
ArrayList areaList = new ArrayList();
int i = 0;
for (int iHeight = 0; iHeight < heightCount ; iHeight ++)
{
for (int iWidth = 0; iWidth < widthCount ; iWidth ++)
{
int pointX = iWidth * cropWidth;
int pointY = iHeight * cropHeight;
int areaWidth = ((pointX + cropWidth) > imgWidth) ? (imgWidth - pointX) : cropWidth;
int areaHeight = ((pointY + cropHeight) > imgHeight) ? (imgHeight - pointY) : cropHeight;
string s = string.Format("{0};{1};{2};{3}",pointX,pointY,areaWidth,areaHeight);
Rectangle rect = new Rectangle(pointX,pointY,areaWidth,areaHeight);
areaList.Add(rect);
i ++;
}
}
for (int iLoop = 0 ; iLoop < areaList.Count ; iLoop ++)
{
Rectangle rect = (Rectangle)areaList[iLoop];
string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + iLoop.ToString() + this._fileExtension;
Bitmap newBmp = new Bitmap(rect.Width,rect.Height,PixelFormat.Format24bppRgb);
Graphics newBmpGraphics = Graphics.FromImage(newBmp);
newBmpGraphics.DrawImage(inputImg,new Rectangle(0,0,rect.Width,rect.Height),rect,GraphicsUnit.Pixel);
newBmpGraphics.Save();
switch (this._fileExtension.ToLower())
{
case ".jpg":
case ".jpeg":
newBmp.Save(fileName,ImageFormat.Jpeg);
break;
case "gif":
newBmp.Save(fileName,ImageFormat.Gif);
break;
}
}
inputImg.Dispose();
}
}