C#画图GDI Grphic
http://hi.baidu.com/crp8/blog/item/f374944431157346500ffe24.html
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本
//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
//PathGradientBrush路径渐变,LinearGradientBrush线性渐变
Image bgimage = new Bitmap("E:2065215396.jpg");
brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景
g.FillEllipse(brush,50,50,500,300);
//申请画图区域System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
//创建笔
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格
//创建单色画刷
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//画图函数
protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
}
//颜色对话框
ColorDialog c = new ColorDialog();
c.ShowDialog();
textBox1.BackColor = c.Color;
//字体对话框
FontDialog f = new FontDialog();
f.ShowDialog();
textBox1.Font = flg.Font;
//RGB的使用
int Red=(int)SelfPlaceKey.GetValue("Red");
int Green=(int)SelfPlaceKey.GetValue("Green");
int Blue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);
//字体
Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);
rect=new Rectangle(0,y,400,aFont.Height);
g.DrawRectangle(Pens.Blue,rect);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Far;
g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);
y+=aFont.Height+20;
aFont.Dispose();
===============================
对于GDI+,在正常的操作,Bitmap-- Graphcis -- DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定..尤其是在读取图片后,生成缩略图之后,文件会被压缩而失真..
主要原因是因为没有重新设置Graphics的几个属性..
1.Graphics.SmoothingMode属性: 例如SmoothingMode.HighQuality可以产生高质量图片,但是效率低.
2.Graphics.CompositingQuality 属性: 例如:CompositingQuality.HighQuality也是产生高质量图,效率低下.
3.Graphics.InterpolationMode 属性,例如:InterpolationMode.HighQualityBicubic与前两个也是同样的效果.
这三个属性的值都是enum,具体的enum参数可以查看MSDN的说明..在这里就我不赘述..
如果是对图片进行放大,缩小,可以调整Graphics.CompositingQuality 和Graphics.InterpolationMode 两个属性..如果是图片生成,则可以调整Graphics.SmoothingMode属性..
另外一个问题就是关于文字生成的..按照正常的模式生成的文字,可以很明显的看到文字带有锯齿..解决的办法也是需要修改Graphics的一个属性: Graphics.TextRenderingHint...注意一点,修改TextRenderingHint的话,需要引入System.Drawing.Text,例如:Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
经过对这四个属性的修改,操作大部分的图片之后,产生的结果都是比较让人满意的..
=========================================
c#处理绘制图像的时候超出窗体的大小时,窗体自动添加滚动条;当滚动条移动的时候重写绘制隐藏的部分
http://hi.baidu.com/jobsmeng/blog/item/a8732c361ebddf370b55a96e.html
注:窗体要设置最小滚动:this.AutoScrollMinSize = new Size(528, 371); =》其中528指的是宽度、371指的是高度。
下面两部分,上面部分有问题;后面部分是正确的方法;
base.OnPaint(e);
Graphics dc = e.Graphics; // member fields
if (e.ClipRectangle.Top < 528 || e.ClipRectangle.Left < 371)
{
Rectangle rectangleArea = new Rectangle(rectangleTopLeft, rectangleSize);
Rectangle ellipseArea = new Rectangle(ellipseTopLeft, ellipseSize);
dc.DrawRectangle(bluePen, rectangleArea);
dc.DrawEllipse(redPen, ellipseArea);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Graphics dc2 = e.Graphics;
Size scrollOffset = new Size(this.AutoScrollPosition);//获得需要重新绘制的区域,这是滚动的距离,它存储在Form类的一个属性AutoScrollPosition中.
//TranlateTransform.提供水平和垂直坐标,表示客户区域的左上角相对于文档的左上角,然后Graphics设备考虑客户区域相对于文档区域的位置,计算这些坐标.
//dc2.TranslateTranform(this.AutoScrollPosition.X,this.AutoScrollPosition.Y);
if (e.ClipRectangle.Top + scrollOffset.Width < 528 || e.ClipRectangle.Left + scrollOffset.Height < 371)
{
Rectangle rectangleArea = new Rectangle(rectangleTopLeft + scrollOffset, ellipseSize);
dc2.DrawRectangle(bluePen, rectangleArea);
dc2.DrawEllipse(redPen, rectangleArea);
}
=================================================
using System;
using System.Drawing;
namespace ImageProc
{
/// <summary>
/// This Class is Written Specifically for Grey Scale & Binary Images
/// All the function Take Bitmap as input and Return Bitmap as output
/// </summary>
public class Processing
{
public Processing()
{
}
/// <summary>
/// It is the Wiener Filter Function
/// </summary>
/// <param name="use">It is of Type Bitmap </param>
/// <returns>Return Bitmap after Applying Wiener Filter to it.</returns>
public static Bitmap Wiener(Bitmap use)
{
Bitmap wien= new Bitmap(use.Height,use.Width);
//Variable's used for Calculations
int i,j,N;
uint tmp=0;
uint mM,M1,mVAR;
// Calculating the Total Mean of the image
for(i=0;i< use.Height;i++)
{
for(j=0;j<use.Width-1;j++)
{
Color clr = use.GetPixel(i,j);
tmp = tmp + Convert.ToUInt32(clr.R);
}
}
N = use.Height*use.Width;
mM=Convert.ToUInt32( Math.Abs(tmp/N));
// Calculating the Total Variance of the image
tmp=0;
for(i=0;i<use.Height ;i++)
{
for(j=0;j<use.Width;j++)
{
Color clr = use.GetPixel(i,j);
M1= ((Convert.ToUInt32(clr.R)-mM)*(Convert.ToUInt32(clr.R)-mM));
tmp = tmp + M1;
}
}
mVAR=Convert.ToUInt32( Math.Abs(tmp/N));
//Convert the image to integer array
uint[,] image = new uint[300,300];
for(i=0;i<use.Height ;i++)
{
for(j=0;j<use.Width;j++)
{
image[i,j] = Convert.ToUInt32(use.GetPixel(i,j).R);
}
}
int Block = 5;
int BlockSqr = 25;
// Internal Block Processing
for(i=Block;i < use.Height-Block;i=i+1)
for(j=Block; j < use.Width-Block;j=j+1)
{
int lM,lVAR;
// Calculating the Mean of the Block
tmp= 0;
for(int k=i-Block;k<i+Block ;k=k+1)
{
for(int l=j-Block;l<j+Block;l=l+1)
{
tmp = tmp +image[k,l];
}
}
lM=Convert.ToInt32( Math.Abs(tmp/BlockSqr));
// Calculating the Variance of the Block
tmp=0;
M1=0;
for(int k=i-Block;k< Block+i ;k=k+1)
{
for(int l=j-Block;l<Block+j ;l=l+1)
{
M1= ((image[k,l]-Convert.ToUInt32(lM))*(image[k,l]-Convert.ToUInt32(lM)));
tmp = tmp + M1;
}
}
lVAR=Convert.ToInt32( Math.Abs(tmp/BlockSqr));
//Putting the filtered value
float tm =(lVAR-mVAR);
float tm1 = tm/lVAR;
int mm = Convert.ToInt32(image[i,j]-lM);
float tm2 = tm1 * (image[i,j]-lM);
int A = lM+Convert.ToInt32(tm2);
if(A<255)
wien.SetPixel(i,j,Color.FromArgb(255,A,A,A));
}
return(wien);
}
/// <summary>
/// Applying LowPass Filter To the Bitmap
/// </summary>
/// <param name="use">Takes Bitmap as the input</param>
/// <returns>Returns Bitmap as the output after Performing Lowpass filtering</returns>
public static Bitmap LowPass(Bitmap use)
{
int Rows = use.Height;
int Cols = use.Width;
int pixel=0;
Bitmap LowFilter = new Bitmap(use.Height,use.Width);
for (int r=1; r< Rows-1; r++)
for (int c=1; c< Cols-1; c++)
{
Color z1 = use.GetPixel(r-1,c-1);
Color z2 = use.GetPixel(r-1,c);
Color z3 = use.GetPixel(r-1,c+1);
Color z4 = use.GetPixel(r,c-1);
Color z5 = use.GetPixel(r,c);
Color z6 = use.GetPixel(r,c+1);
Color z7 = use.GetPixel(r+1,c-1);
Color z8 = use.GetPixel(r+1,c);
Color z9 = use.GetPixel(r+1,c+1);
/* Apply Lowpass operator. */
pixel = Convert.ToInt32(z3.R) +Convert.ToInt32(z1.R) +Convert.ToInt32(z6.R)+Convert.ToInt32(z4.R) +Convert.ToInt32(z9.R) +Convert.ToInt32(z7.R)+Convert.ToInt32(z2.R)+Convert.ToInt32(z5.R)+Convert.ToInt32(z8.R) ;
pixel = pixel/9;
/* Check magnitude */
//if (pixel <255)
LowFilter.SetPixel(r,c,Color.FromArgb(255,pixel,pixel,pixel));
}
return(LowFilter);
}
/// <summary>
/// Convert Bitmap from Grey to Binary Using Threshold Method using Mean
/// </summary>
/// <param name="use">Takes Bitmap as Input</param>
/// <returns>Return Binary Image </returns>
public static Bitmap ThreshMean(Bitmap use)
{
Bitmap ThreshHold = new Bitmap(use.Height,use.Width);
int Rows = 15;
int Cols = 15;
for(int i=0;i < use.Height;i=i+15)
for(int j=0; j < use.Width;j=j+15)
{
int r,c;
int pixel = 0;
int[] arr = new int[255];
int cnt = 0;
for ( r=i; r< Rows+i ; r++)
for (c=j; c< Cols+j ; c++)
{
Color z1 = use.GetPixel(r,c);
arr[cnt] = Convert.ToInt32(z1.R);
cnt++;
}
for(int k=0;k<255;k++)
pixel = pixel+arr[k];
pixel = pixel/255;
for ( r=i; r< Rows+i ; r++)
for (c=j; c< Cols+j ; c++)
{
//Color z1 = hist.GetPixel(r,c);
Color z1 = use.GetPixel(r,c);
if(pixel < Convert.ToInt32(z1.R))
ThreshHold.SetPixel(r,c,Color.FromArgb(255,0,0,0));
else
ThreshHold.SetPixel(r,c,Color.FromArgb(255,255,255,255));
}
}
return(ThreshHold);
}
/// <summary>
/// Does Histogram Processing on the Grey Scale Image
/// </summary>
/// <param name="use">Takes Bitmap as Input</param>
/// <returns>Return Bitmap which is Histogram Processed</returns>
public static Bitmap Histogram(Bitmap use)
{
int r,c,tmp=0;
float[] Pixel= new float[256];
float[] Prob = new float[256];
//Calculating the number of pixel of same type
for(r=0;r<use.Height;r++)
{
for(c=0;c<use.Width;c++)
{
Color cr = use.GetPixel(r,c);
tmp = Convert.ToInt32(cr.R);
Pixel[tmp]=Pixel[tmp]+1;
}
}
//Total Number of Pixels
int tot = use.Height*use.Width;
//Calculating the probability
for(r=0;r<256;r++)
{
if(Pixel[r]!=0)
{
Pixel[r]=Pixel[r]/tot;
}
}
//Calculating the cumulative probability
Prob[0]=Pixel[0];
for(r=1;r<256;r++)
{
Prob[r]=Prob[r-1]+Pixel[r];
}
// Generating New Graylevel Values
for(r=0;r<256;r++)
{
Prob[r]=Prob[r]*255;
}
Bitmap hist = new Bitmap(use.Height,use.Width);
for(r=0;r<use.Height;r++)
{
for(c=0;c<use.Width;c++)
{
Color cr = use.GetPixel(r,c);
tmp = Convert.ToInt32(cr.R);
hist.SetPixel(r,c,Color.FromArgb(255,Convert.ToInt32(Prob[tmp]),Convert.ToInt32(Prob[tmp]),Convert.ToInt32(Prob[tmp])));
}
}
//End of Histogram
return(hist);
}
/// <summary>
/// Does the Normalization of the Grey Scale Image
/// </summary>
/// <param name="use">Takes Bitmap as Input</param>
/// <returns>Returns Normalized Bitmap</returns>
public static Bitmap Normalize(Bitmap use)
{
Bitmap normalized = new Bitmap(use.Height,use.Width);
//Variable's used for Calculations
int i,j,N;
uint tmp=0;
uint M,M1,VAR;
// Calculating the Mean of the image
for(i=0;i< use.Height -1;i++)
{
for(j=0;j<use.Width-1;j++)
{
Color clr = use.GetPixel(i,j);
tmp = tmp + Convert.ToUInt32(clr.R);
}
}
N = use.Height*use.Width;
M=Convert.ToUInt32( Math.Abs(tmp/N));
// Calculating the Variance of the image
tmp=0;
for(i=0;i<use.Height -1;i++)
{
for(j=0;j<use.Width-1;j++)
{
Color clr = use.GetPixel(i,j);
M1= ((Convert.ToUInt32(clr.R)-M)*(Convert.ToUInt32(clr.R)-M));
tmp = tmp + M1;
}
}
VAR=Convert.ToUInt32( Math.Abs(tmp/N));
uint MO =100,VARO =100,newInten;
Color Inten;
for(i=0;i<use.Height -1;i++)
{
for(j=0;j<use.Width-1;j++)
{
Inten = use.GetPixel(i,j);
if(Convert.ToUInt32(Inten.R) > M)
{
newInten = MO + Convert.ToUInt32(Math.Sqrt((VARO*((Convert.ToUInt32(Inten.R)-M)*(Convert.ToUInt32(Inten.R)-M)))/VAR));
normalized.SetPixel(i,j,Color.FromArgb(255,Convert.ToInt32(newInten),Convert.ToInt16(newInten),Convert.ToInt16(newInten)));
}
else
{
newInten = MO - Convert.ToUInt32(Math.Sqrt((VARO*((Convert.ToUInt32(Inten.R)-M)*(Convert.ToUInt32(Inten.R)-M)))/VAR));
normalized.SetPixel(i,j,Color.FromArgb(255,Convert.ToInt32(newInten),Convert.ToInt16(newInten),Convert.ToInt16(newInten)));
}
}
}
return(normalized);
//End of Normalization
}
/// <summary>
/// This Function Does Sekelotonization to the Binary Image
/// </summary>
/// <param name="use">Takes Binary Image as Input</param>
/// <returns>Return Thined Binar Image</returns>
public static Bitmap ThinMean(Bitmap use)
{
bool success3 = true;
while(success3)
{
bool success = false ;
bool success2 = false;
int count ;
int count2;
int num_transitions ;
int[,] picture = new int[300,300];
int[,] temp_array= new int[300,300];
int transitions ;
int[,] temp = new int[300,300];
for (int h = 0; h < 300; h++)
for (int w = 0; w < 300; w++)
{
picture[h,w] =Convert.ToInt32(use.GetPixel(h,w).R);
}
for (int i = 1; i < 300-1; i++)
{
for (int j = 1; j < 300-1; j++)
{
count = 0;
num_transitions = 0;
// check for N(p)
if (picture[i,j] == 255)
{
if (picture[i-1,j-1] != 0)
count++;
if (picture[i,j-1] != 0)
count++;
if (picture[i+1,j-1] != 0)
count++;
if (picture[i+1,j] != 0)
count++;
if (picture[i-1,j] != 0)
count++;
if (picture[i+1,j+1] != 0)
count++;
if (picture[i,j+1] != 0)
count++;
if (picture[i-1,j+1] != 0)
count++;
if (count != 8)
{
// 2 <= N(p) <= 6
if (count >= 2 && count <= 6)
{
if(picture[i-1,j] == 0 && picture[i-1,j+1] == 255)
num_transitions++ ;
if(picture[i-1,j+1] == 0 && picture[i,j+1] == 255)
num_transitions++ ;
if(picture[i,j+1] == 0 && picture[i+1,j+1] == 255)
num_transitions++ ;
if(picture[i+1,j+1] == 0 && picture[i+1,j] == 255)
num_transitions++ ;
if(picture[i+1,j] == 0 && picture[i+1,j-1] == 255)
num_transitions++ ;
if(picture[i+1,j-1] == 0 && picture[i,j-1] == 255)
num_transitions++ ;
if(picture[i,j-1] == 0 && picture[i-1,j-1] == 255)
num_transitions++ ;
if(picture[i-1,j-1] == 0 && picture[i-1,j] == 255)
num_transitions++ ;
//S(p) = 1
if (num_transitions == 1)
{
// if p2 * p4 * p6 = 0
if (picture[i-1,j] == 0 || picture[i,j+1] == 0 ||
picture[i+1,j] == 0)
{
// if p4 * p6 * p8 = 0
if(picture[i,j+1] == 0 || picture [i+1,j] == 0 ||picture[i,j-1] == 0)
{
temp_array[i,j] = 0;
success = true;
}
else
temp_array[i,j] = 255;
}
else
temp_array[i,j] = 255;
}
else
temp_array[i,j] = 255 ;
}
else
temp_array[i,j] = 255 ;
}
else
temp_array[i,j] = 255;
}
else
temp_array[i,j] = 0;
}
}
//copy thinned image back to original
for (int a = 0; a < 300; a++)
{
for (int b = 0; b < 300; b++)
picture[a,b] = temp_array[a,b];
}
// step 2 of the thinning algorithm
for (int k = 0; k < 300; k++)
{
for (int l = 0; l < 300; l++)
{
count2 = 0;
transitions = 0;
if (picture[k,l] == 255)
{
if (picture[k-1,l-1] != 0)
count2++;
if (picture[k,l-1] != 0)
count2++;
if (picture[k+1,l-1] != 0)
count2++;
if (picture[k+1,l] != 0)
count2++;
if (picture[k-1,l] != 0)
count2++;
if (picture[k+1,l+1] != 0)
count2++;
if (picture[k,l+1] != 0)
count2++;
if (picture[k-1,l+1] != 0)
count2++;
if (count2 != 8)
{
if (count2 >= 2 && count2 <= 6)
{
if(picture[k-1,l] == 0 && picture[k-1,l+1] == 255)
transitions++ ;
if(picture[k-1,l+1] == 0 && picture[k,l+1] == 255)
transitions++ ;
if(picture[k,l+1] == 0 && picture[k+1,l+1] == 255)
transitions++ ;
if(picture[k+1,l+1] == 0 && picture[k+1,l] == 255)
transitions++ ;
if(picture[k+1,l] == 0 && picture[k+1,l-1] == 255)
transitions++ ;
if(picture[k+1,l-1] == 0 && picture[k,l-1] == 255)
transitions++ ;
if(picture[k,l-1] == 0 && picture[k-1,l-1] == 255)
transitions++ ;
if(picture[k-1,l-1] == 0 && picture[k-1,l] == 255)
transitions++ ;
if (transitions == 1)
{
// if p2 * p4 * p8 = 0
if(picture[k-1,l] == 0 || picture[k,l+1] == 0 ||picture[k,l-1] == 0)
{
// if p2 * p6 * p8
if(picture[k-1,l] == 0 || picture[k+1,l] == 0 ||picture[k,l-1] == 0)
{
temp[k,l] = 0;
success2 = true;
}
else
temp[k,l] = 255;
}
else
temp[k,l] = 255;
}
else
temp[k,l] = 255;
}
else
temp[k,l] = 255;
}
else
temp[k,l] = 255;
}
else
temp[k,l] = 0;
}
}
for (int a = 0; a < 300; a++)
for (int b = 0; b < 300; b++)
{
int tmp = temp[a,b];
use.SetPixel(a,b,Color.FromArgb(255,tmp,tmp,tmp));
}
if(success || success2)
success3= true;
else
success3= false;
}
return(use);
}
/// <summary>
/// Applys Sobel Operator to the Grey Scale Image
/// </summary>
/// <param name="use">Takes Grey Scale Image as Input</param>
/// <returns>Return Grey Scale Image after applying Sobel Operator </returns>
public static Bitmap Sobel(Bitmap use)
{
int Rows = use.Height;
int Cols = use.Width;
int r,c;
int pixel;
Bitmap sobel = new Bitmap(Rows,Cols);
Bitmap gx = new Bitmap(Rows,Cols);
Bitmap gy = new Bitmap(Rows,Cols);
pixel=0;
for (r=1; r< Rows-1; r++)
for (c=1; c< Cols-1; c++)
{
int[] z = new Int32[10];
z[1] = Convert.ToInt32(use.GetPixel(r-1,c-1).R);
z[3] = Convert.ToInt32(use.GetPixel(r-1,c+1).R);
z[4] = Convert.ToInt32(use.GetPixel(r,c-1).R);
z[6] = Convert.ToInt32(use.GetPixel(r,c+1).R);
z[7] = Convert.ToInt32(use.GetPixel(r+1,c-1).R);
z[9] = Convert.ToInt32(use.GetPixel(r+1,c+1).R);
/* Apply Sobel operator. */
pixel = z[3] -z[1] +(2*z[6] )-(2*z[4] )+z[9] -z[7] ;
/* Normalize and take absolute value */
pixel = Math.Abs(pixel);
/* Check magnitude */
if (pixel <255)
gy.SetPixel(r,c,Color.FromArgb(255,pixel,pixel,pixel));
}
pixel =0;
for (r=1; r< Rows-1; r++)
for (c=1; c< Cols-1; c++)
{
int[] z = new Int32[10];
z[1] = Convert.ToInt32(use.GetPixel(r-1,c-1).R) ;
z[3] = Convert.ToInt32(use.GetPixel(r-1,c+1).R) ;
z[2] = Convert.ToInt32(use.GetPixel(r-1,c).R) ;
z[8] = Convert.ToInt32(use.GetPixel(r+1,c).R) ;
z[7] = Convert.ToInt32(use.GetPixel(r+1,c-1).R) ;
z[9] = Convert.ToInt32(use.GetPixel(r+1,c+1).R) ;
/* Apply Sobel operator. */
pixel = z[7]-z[1]+(2*z[8])-(2*z[2])+z[9]-z[3];
/* Normalize and take absolute value */
pixel = Math.Abs(pixel);
/* Check magnitude */
if (pixel <255)
gx.SetPixel(r,c,Color.FromArgb(255,pixel,pixel,pixel));
}
pixel =0;
for(r=0;r<Rows;r++)
for(c=0;c<Cols;c++)
{
Color x = gx.GetPixel(r,c);
Color y = gy.GetPixel(r,c);
pixel = Convert.ToInt32(x.R) + Convert.ToInt32(y.R );
if (pixel <255)
sobel.SetPixel(r,c,Color.FromArgb(255,pixel,pixel,pixel));
}
return(sobel);
}
}
}