c#下分割圖像的幾種辦法
Bitmap bb = (Bitmap)Image.FromFile("...");
Bitmap sepbitmap1= bb.Clone(new System.Drawing.Rectangle(0,0,20,20),System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Image image = System.Drawing.Image.FromFile("a.jpg",true);
Bitmap bmp = new Bitmap(400,400);
Graphics grap = Graphics.FromImage(bmp);
grap.Clear(Color.Transparent);
grap.DrawImage(image,new Rectangle(0,0,400,400));
如果a.jpg的大小是4000*4000,這樣在新的圖像中得到的就是左上角起的400*400的一部份.
若要10*10,則用循環就可實現
[System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , // 目標 DC的句柄
int nXDest ,
int nYDest ,
int nWidth ,
int nHeight ,
IntPtr hdcSrc , // 源DC的句柄
int nXSrc ,
int nYSrc ,
System.Int32 dwRop // 光柵的處理數值
) ;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string file = null;
Bitmap bmp = null;
IntPtr dc1 = IntPtr.Zero;
IntPtr dc2 = IntPtr.Zero;
int row = 2, col = 2;
int left = 0, top =0 , width = 0, height = 0;
for(int i=0; i<row; i++)
{
left = 0;
top = i * ((int)(this.pictureBox1.Height / row));
for(int j=0; j<col; j++)
{
file = @"c:/"+ i.ToString() + j.ToString() +".bmp";
if (j == col-1)
width = this.pictureBox1.Width - ((int)(this.pictureBox1.Width / col)) * j;
else
width = (int)(this.pictureBox1.Width / col);
if (i == row-1)
height = this.pictureBox1.Height - ((int)(this.pictureBox1.Height / row)) * i;
else
height = (int)(this.pictureBox1.Height / row);
Graphics g1 = this.pictureBox1.CreateGraphics();
bmp = new Bitmap(width, height, g1);
Graphics g2 = Graphics.FromImage(bmp);
dc1 = g1.GetHdc();
dc2 = g2.GetHdc();
BitBlt (dc2 , 0 , 0 , width , height , dc1 , left , top , 13369376 ) ;
g1.ReleaseHdc(dc1) ;
g2.ReleaseHdc(dc2) ;
g1.Dispose();
g2.Dispose();
bmp.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
left += width;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Bitmap sepbitmap1= bb.Clone(new System.Drawing.Rectangle(0,0,20,20),System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Image image = System.Drawing.Image.FromFile("a.jpg",true);
Bitmap bmp = new Bitmap(400,400);
Graphics grap = Graphics.FromImage(bmp);
grap.Clear(Color.Transparent);
grap.DrawImage(image,new Rectangle(0,0,400,400));
如果a.jpg的大小是4000*4000,這樣在新的圖像中得到的就是左上角起的400*400的一部份.
若要10*10,則用循環就可實現
[System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , // 目標 DC的句柄
int nXDest ,
int nYDest ,
int nWidth ,
int nHeight ,
IntPtr hdcSrc , // 源DC的句柄
int nXSrc ,
int nYSrc ,
System.Int32 dwRop // 光柵的處理數值
) ;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string file = null;
Bitmap bmp = null;
IntPtr dc1 = IntPtr.Zero;
IntPtr dc2 = IntPtr.Zero;
int row = 2, col = 2;
int left = 0, top =0 , width = 0, height = 0;
for(int i=0; i<row; i++)
{
left = 0;
top = i * ((int)(this.pictureBox1.Height / row));
for(int j=0; j<col; j++)
{
file = @"c:/"+ i.ToString() + j.ToString() +".bmp";
if (j == col-1)
width = this.pictureBox1.Width - ((int)(this.pictureBox1.Width / col)) * j;
else
width = (int)(this.pictureBox1.Width / col);
if (i == row-1)
height = this.pictureBox1.Height - ((int)(this.pictureBox1.Height / row)) * i;
else
height = (int)(this.pictureBox1.Height / row);
Graphics g1 = this.pictureBox1.CreateGraphics();
bmp = new Bitmap(width, height, g1);
Graphics g2 = Graphics.FromImage(bmp);
dc1 = g1.GetHdc();
dc2 = g2.GetHdc();
BitBlt (dc2 , 0 , 0 , width , height , dc1 , left , top , 13369376 ) ;
g1.ReleaseHdc(dc1) ;
g2.ReleaseHdc(dc2) ;
g1.Dispose();
g2.Dispose();
bmp.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
left += width;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.