使用图片做窗体怎么做啊

解决方案 »

  1.   

    1.添加一个实现文件,内容如下 namespace IrregularFrom {     public class BitmapRegion     {         public BitmapRegion()         { }           public static void CreateControlRegion(Control control, Bitmap bitmap)         {             if (control == null || bitmap == null)                 return;               control.Width = bitmap.Width;             control.Height = bitmap.Height;               if (control is System.Windows.Forms.Form)             {                 Form form = (Form)control;                 form.Width = control.Width;                 form.Height = control.Height;                 form.FormBorderStyle = FormBorderStyle.None;                 form.BackgroundImage = bitmap;                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 form.Region = new Region(graphicsPath);             }             else if(control is System.Windows.Forms.Button)             {                 Button button = (Button)control;                 button.Text = "";                 button.Cursor = Cursors.Hand;                 button.BackgroundImage = bitmap;                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 button.Region = new Region(graphicsPath);             }         }           private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)         {             GraphicsPath graphicsPath = new GraphicsPath();             Color colorTransparent = bitmap.GetPixel(0, 0);             int colOpaquePixel = 0;             for (int row = 0; row < bitmap.Height; row++)             {                 colOpaquePixel = 0;                 for (int col = 0; col < bitmap.Width; col++)                 {                     if (bitmap.GetPixel(col, row) != colorTransparent)                     {                         colOpaquePixel = col;                         int colNext = col;                         for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)                         {                             if (bitmap.GetPixel(colNext, row) == colorTransparent)                                 break;                         }                         graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));                         col = colNext;                     }                 }             }             return graphicsPath;         }     } }   2.方法的使用 Bitmap bmp = Resource2.Image1 as Bitmap;//使用资源文件             BitmapRegion.CreateControlRegion(this, bmp);//窗体                            Bitmap b = new Bitmap("../../wjx2.jpg");//使用本地文件             BitmapRegion.CreateControlRegion(this.button1, b);//按钮 
      

  2.   

    1.添加一个实现文件,内容如下 namespace IrregularFrom {     public class BitmapRegion     {         public BitmapRegion()         { }           public static void CreateControlRegion(Control control, Bitmap bitmap)         {             if (control == null || bitmap == null)                 return;               control.Width = bitmap.Width;             control.Height = bitmap.Height;               if (control is System.Windows.Forms.Form)             {                 Form form = (Form)control;                 form.Width = control.Width;                 form.Height = control.Height;                 form.FormBorderStyle = FormBorderStyle.None;                 form.BackgroundImage = bitmap;                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 form.Region = new Region(graphicsPath);             }             else if(control is System.Windows.Forms.Button)             {                 Button button = (Button)control;                 button.Text = "";                 button.Cursor = Cursors.Hand;                 button.BackgroundImage = bitmap;                 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 button.Region = new Region(graphicsPath);             }         }           private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)         {             GraphicsPath graphicsPath = new GraphicsPath();             Color colorTransparent = bitmap.GetPixel(0, 0);             int colOpaquePixel = 0;             for (int row = 0; row < bitmap.Height; row++)             {                 colOpaquePixel = 0;                 for (int col = 0; col < bitmap.Width; col++)                 {                     if (bitmap.GetPixel(col, row) != colorTransparent)                     {                         colOpaquePixel = col;                         int colNext = col;                         for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)                         {                             if (bitmap.GetPixel(colNext, row) == colorTransparent)                                 break;                         }                         graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));                         col = colNext;                     }                 }             }             return graphicsPath;         }     } }   2.方法的使用 Bitmap bmp = Resource2.Image1 as Bitmap;//使用资源文件             BitmapRegion.CreateControlRegion(this, bmp);//窗体                            Bitmap b = new Bitmap("../../wjx2.jpg");//使用本地文件             BitmapRegion.CreateControlRegion(this.button1, b);//按钮 
      

  3.   

    1.添加一个实现文件,内容如下
     namespace IrregularFrom {     
    public class BitmapRegion     {        
     public BitmapRegion()         { }          
     public static void CreateControlRegion(Control control, Bitmap bitmap)         {             if (control == null || bitmap == null)                 return;               
    control.Width = bitmap.Width;             
    control.Height = bitmap.Height;              
     if (control is System.Windows.Forms.Form)             
    {                 
    Form form = (Form)control;                 
    form.Width = control.Width;                 
    form.Height = control.Height;                
     form.FormBorderStyle = FormBorderStyle.None;                
     form.BackgroundImage = bitmap;                
     GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 form.Region = new Region(graphicsPath);            
     }             else if(control is System.Windows.Forms.Button)             {                 
    Button button = (Button)control;                
     button.Text = "";                 
    button.Cursor = Cursors.Hand;                
     button.BackgroundImage = bitmap;                
     GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap);                 button.Region = new Region(graphicsPath);            
     }        
     }          
     private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap)         {             
    GraphicsPath graphicsPath = new GraphicsPath();         
        Color colorTransparent = bitmap.GetPixel(0, 0);      
           int colOpaquePixel = 0;           
      for (int row = 0; row < bitmap.Height; row++)             {                 colOpaquePixel = 0;              
       for (int col = 0; col < bitmap.Width; col++)                 {                     
    if (bitmap.GetPixel(col, row) != colorTransparent)                     {                       
      colOpaquePixel = col;                  
           int colNext = col;                  
           for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++)                     
        {                           
      if (bitmap.GetPixel(colNext, row) == colorTransparent)                                 break;                       
      }                         
    graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1));                      
       col = colNext;                   
      }                 }           
      }            
     return graphicsPath;     
        }   
      }
     }   
    2.方法的使用
     Bitmap bmp = Resource2.Image1 as Bitmap;//使用资源文件             BitmapRegion.CreateControlRegion(this, bmp);//窗体                       
         Bitmap b = new Bitmap("../../wjx2.jpg");//使用本地文件             BitmapRegion.CreateControlRegion(this.button1, b);//按钮 
      

  4.   

    这个用Jquery可以实现吧,前段时间的看的视频教程里有类似的例子,忘记怎么实现了,只是给楼主提供种思路
      

  5.   

    下面的是VB的,可是转换成C#运行有问题#Region "使用图片画窗体"    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Using MyGp As New Drawing2D.GraphicsPath
                    MyGp.FillMode = Drawing2D.FillMode.Winding
                    MyGp.AddPolygon(GetPix(Me))
                    Me.Region = New Region(MyGp)
                End Using
                '====================================================
            Catch ex As Exception
                MessageBox.Show(ex.Message, "错误")
                Me.DialogResult = Windows.Forms.DialogResult.Cancel
            End Try
        End Sub    Private Function GetPix(ByVal Form As Control) As Point()
            Dim MyPoint() As Point = Nothing
            '   Try
            Using MyImage As New Drawing.Bitmap(My.Resources.Dark_GreenEarth_1)
                Dim sum As Integer = 0
                Dim MyControlImage As Bitmap = MyImage.GetThumbnailImage(Form.Width, Form.Height, Nothing, IntPtr.Zero)
                Form.BackgroundImage = MyControlImage
                '****************************************************
                ' 这里的代码最重要
                ' 注意:数组下标坐标点必须按照线条的顺序,否则则会出错
                For x As Integer = 1 To MyControlImage.Width - 1
                    For y As Integer = 1 To MyControlImage.Height - 1
                        If MyControlImage.GetPixel(x, y).A > 125 Then  ' 若显示有少许差错 请将 0 替换为 0~254 任意试试
                            ReDim Preserve MyPoint(sum)
                            MyPoint(sum).X = x
                            MyPoint(sum).Y = y
                            sum += 1
                            Exit For
                        End If
                    Next
                Next            For x As Integer = MyControlImage.Width - 1 To 1 Step -1
                    For y As Integer = MyControlImage.Height - 1 To 1 Step -1
                        If MyControlImage.GetPixel(x, y).A > 125 Then
                            ReDim Preserve MyPoint(sum)  ' 若显示有少许差错 请将 0 替换为 1~255 任意试试
                            MyPoint(sum).X = x
                            MyPoint(sum).Y = y
                            sum += 1
                            Exit For
                        End If
                    Next
                Next            ' 由于时间仓促,不免有多少错误之处~
                ' 必须注意的是,像GIF,PNG格式背影为透明的图片,可能在某一段区域透明度为5,可能可能在某一段区域透明度为10......
                ' 请将代码按自已的需要优化
                '****************************************************        End Using
            Return MyPoint
            '  Catch ex As Exception
            'MessageBox.Show(ex.Message)
            ' Return MyPoint
            ' End Try
        End Function
    #End Region