一、各種旋轉(zhuǎn)、改變大小注意:先要添加畫(huà)圖相關(guān)的using引用。//向右旋轉(zhuǎn)圖像90°代碼如下:PRivate void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");//加載圖像g.FillRectangle(Brushes.White, this.ClientRectangle);//填充窗體背景為白色Point[] destinationPoints = {new Point(100, 0), // destination for upper-left point of originalnew Point(100, 100),// destination for upper-right point of originalnew Point(0, 0)}; // destination for lower-left point of originalg.DrawImage(bmp, destinationPoints);}//旋轉(zhuǎn)圖像180°代碼如下:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");g.FillRectangle(Brushes.White, this.ClientRectangle);Point[] destinationPoints = {new Point(0, 100), // destination for upper-left point of originalnew Point(100, 100),// destination for upper-right point of originalnew Point(0, 0)}; // destination for lower-left point of originalg.DrawImage(bmp, destinationPoints);}//圖像切變代碼:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");g.FillRectangle(Brushes.White, this.ClientRectangle);Point[] destinationPoints = {new Point(0, 0), // destination for upper-left point of originalnew Point(100, 0), // destination for upper-right point of originalnew Point(50, 100)};// destination for lower-left point of originalg.DrawImage(bmp, destinationPoints);}//圖像截取:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");g.FillRectangle(Brushes.White, this.ClientRectangle);Rectangle sr = new Rectangle(80, 60, 400, 400);//要截取的矩形區(qū)域Rectangle dr = new Rectangle(0, 0, 200, 200);//要顯示到Form的矩形區(qū)域g.DrawImage(bmp, dr, sr, GraphicsUnit.Pixel);}//改變圖像大小:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");g.FillRectangle(Brushes.White, this.ClientRectangle);int width = bmp.Width;int height = bmp.Height;// 改變圖像大小使用低質(zhì)量的模式g.InterpolationMode = InterpolationMode.NearestNeighbor;g.DrawImage(bmp, new Rectangle(10, 10, 120, 120), // source rectanglenew Rectangle(0, 0, width, height), // destination rectangleGraphicsUnit.Pixel);// 使用高質(zhì)量模式//g.CompositingQuality = CompositingQuality.HighSpeed;g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.DrawImage(bmp,new Rectangle(130, 10, 120, 120), new Rectangle(0, 0, width, height),GraphicsUnit.Pixel);}//設(shè)置圖像的分辯率:private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Bitmap bmp = new Bitmap("rama.jpg");g.FillRectangle(Brushes.White, this.ClientRectangle);bmp.SetResolution(300f, 300f);g.DrawImage(bmp, 0, 0);bmp.SetResolution(1200f, 1200f);g.DrawImage(bmp, 180, 0);}//用GDI+畫(huà)圖private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics gForm = e.Graphics;gForm.FillRectangle(Brushes.White, this.ClientRectangle);for (int i = 1; i <= 7; ++i){//在窗體上面畫(huà)出橙色的矩形Rectangle r = new Rectangle(i*40-15, 0, 15,this.ClientRectangle.Height);gForm.FillRectangle(Brushes.Orange, r);}//在內(nèi)存中創(chuàng)建一個(gè)Bitmap并設(shè)置CompositingModeBitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);Graphics gBmp = Graphics.FromImage(bmp);gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;// 創(chuàng)建一個(gè)帶有Alpha的紅色區(qū)域// 并將其畫(huà)在內(nèi)存的位圖里面Color red = Color.FromArgb(0x60, 0xff, 0, 0);Brush redBrush = new SolidBrush(red);gBmp.FillEllipse(redBrush, 70, 70, 160, 160);// 創(chuàng)建一個(gè)帶有Alpha的綠色區(qū)域Color green = Color.FromArgb(0x40, 0, 0xff, 0);Brush greenBrush = new SolidBrush(green);gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);//在窗體上面畫(huà)出位圖 now draw the bitmap on our windowgForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);// 清理資源bmp.Dispose();gBmp.Dispose();redBrush.Dispose();greenBrush.Dispose();}//在窗體上面繪圖并顯示圖像private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){Graphics g = e.Graphics;Pen blackPen = new Pen(Color.Black, 1);if (ClientRectangle.Height / 10 > 0){for (int y = 0; y < ClientRectangle.Height; y += ClientRectangle.Height / 10){g.DrawLine(blackPen, new Point(0, 0), new Point(ClientRectangle.Width, y));}}blackPen.Dispose();} C# 使用Bitmap類進(jìn)行圖片裁剪 在Mapwin(手機(jī)游戲地圖編輯器)生成的地圖txt文件中添加自己需要處理的數(shù)據(jù)后轉(zhuǎn)換成可在手機(jī)(Ophone)開(kāi)發(fā)環(huán)境中使用的字節(jié)流地圖文件的小工具,其中就涉及到圖片的裁剪和生成了。有以下幾種方式。 方法一:拷貝像素。 當(dāng)然這種方法是最笨的,效率也就低了些。在Bitmap類中我們可以看到這樣兩個(gè)方法:GetPixel(int x, int y)和SetPixel(int x, int y, Color color)方法。從字面的含以上就知道前者是獲取圖像某點(diǎn)像素值,是用Color對(duì)象返回的;后者是將已知像素描畫(huà)到制定的位置。下面就來(lái)做個(gè)實(shí)例檢驗(yàn)下:1.首先創(chuàng)建一個(gè)Windows Form窗體程序,往該窗體上拖放7個(gè)PictureBox控件,第一個(gè)用于放置并顯示原始的大圖片,其后6個(gè)用于放置并顯示裁剪后新生成的6個(gè)小圖;2.放置原始大圖的PictureBox控件name屬性命名為pictureBoxBmpRes,其后pictureBox1到pictureBox6依次命名,并放置在合適的位置;3.雙擊Form窗體,然后在Form1_Load事件中加入下面的代碼即可。//導(dǎo)入圖像資源 Bitmap bmpRes = null; String strPath = application.ExecutablePath; try{ int nEndIndex = strPath.LastIndexOf('//'); strPath = strPath.Substring(0,nEndIndex) + "http://Bmp//BmpResMM.bmp"; bmpRes = new Bitmap(strPath); //窗體上顯示加載圖片 pictureBoxBmpRes.Width = bmpRes.Width; pictureBoxBmpRes.Height = bmpRes.Height; pictureBoxBmpRes.Image = bmpRes; } catch(Exception ex) { System.Windows.Forms.MessageBox.Show("圖片資源加載失敗!/r/n" + ex.ToString()); } //裁剪圖片(裁成2行3列的6張圖片) int nYClipNum = 2, nXClipNum = 3; Bitmap[] bmpaClipBmpArr = new Bitmap[nYClipNum * nXClipNum]; for (int nYClipNumIndex = 0; nYClipNumIndex < nYClipNum; nYClipNumIndex++) { for (int nXClipNumIndex = 0; nXClipNumIndex < nXClipNum; nXClipNumIndex++) { int nClipWidth = bmpRes.Width / nXClipNum; int nClipHight = bmpRes.Height / nYClipNum; int nBmpIndex = nXClipNumIndex + nYClipNumIndex * nYClipNum + (nYClipNumIndex > 0?1:0); bmpaClipBmpArr[nBmpIndex] = new Bitmap(nClipWidth, nClipHight); for(int nY = 0; nY < nClipHight; nY++) { for(int nX = 0; nX < nClipWidth; nX++) { int nClipX = nX + nClipWidth * nXClipNumIndex; int nClipY = nY + nClipHight * nYClipNumIndex; Color cClipPixel = bmpRes.GetPixel(nClipX, nClipY); bmpaClipBmpArr[nBmpIndex].SetPixel(nX, nY, cClipPixel); } } } } PictureBox[] picbShow = new PictureBox[nYClipNum * nXClipNum]; picbShow[0] = pictureBox1; picbShow[1] = pictureBox2; picbShow[2] = pictureBox3; picbShow[3] = pictureBox4; picbShow[4] = pictureBox5; picbShow[5] = pictureBox6; for (int nLoop = 0; nLoop < nYClipNum * nXClipNum; nLoop++) { picbShow[nLoop].Width = bmpRes.Width / nXClipNum; picbShow[nLoop].Height = bmpRes.Height / nYClipNum; picbShow[nLoop].Image = bmpaClipBmpArr[nLoop]; } 現(xiàn)在看看那些地方需要注意的了。其中int nBmpIndex =nXClipNumIndex + nYClipNumIndex * nYClipNum + (nYClipNumIndex > 0?1:0); 這句定義了存儲(chǔ)裁剪圖片對(duì)象在數(shù)組中的索引,需要注意的就是后面的(nYClipNumIndex > 0?1:0)——因?yàn)橹挥挟?dāng)裁剪的對(duì)象處于第一行以外的行時(shí)需要將索引加1;另外,因?yàn)檫@種方法的效率不高,程序運(yùn)行起來(lái)還是頓了下。如果有興趣的話,可以將以上的代碼放到一個(gè)按鈕Click事件函數(shù)中,當(dāng)單擊該按鈕時(shí)就可以感覺(jué)到了。 方法二:運(yùn)用Clone函數(shù)局部復(fù)制。 同樣在Bitmap中可以找到Clone()方法,該方法有三個(gè)重載方法。Clone(),Clone(Rectangle, PixelFormat)和Clone(RectangleF, PixelFormat)。第一個(gè)方法將創(chuàng)建并返回一個(gè)精確的實(shí)例對(duì)象,后兩個(gè)就是我們這里需要用的局部裁剪了(其實(shí)后兩個(gè)方法本人覺(jué)得用法上差不多)。將上面的程序稍稍改進(jìn)下——將裁剪的處理放到一個(gè)按鈕事件函數(shù)中,然后再托一個(gè)按鈕好窗體上,最后將下面的代碼復(fù)制到該按鈕的事件函數(shù)中。for (int nYClipNumIndex = 0; nYClipNumIndex < nYClipNum; nYClipNumIndex++){ for (int nXClipNumIndex = 0; nXClipNumIndex < nXClipNum; nXClipNumIndex++) { int nClipWidth = bmpRes.Width / nXClipNum; int nClipHight = bmpRes.Height / nYClipNum;
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注