Csharp/C Sharp by API/System.Drawing/Image

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Image.FromFile(String path)

<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,

  Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information ISBN: 1861007663

  • /

// Change the path(s) if needed. If you have VS.NET write: // "..\\..\\Altamira5.bmp" or @"..\..\Altamira5.bmp" // otherwise // "Altamira5.bmp" // and compile with: // csc Altamira.cs using System; using System.Drawing; using System.Windows.Forms; namespace Altamira { public class Altamira1 : Form {

   Pen p;
   SolidBrush b, bT = new SolidBrush(Color.Black);
   string path = "Altamira5.bmp";  // change the path if needed
   Image im;
   Font f;
   public Altamira1()
   {
       InitializeComponent();
       MyIni();
   }
   private void InitializeComponent()
   {
       this.SuspendLayout();
       this.ClientSize = new System.Drawing.Size(290, 260);
       this.Text = "Altamira";
       this.ResumeLayout(false);
   }
   private void MyIni()
   {
       Color cP = Color.Gray;
       Color cB = Color.LightGray;
       p = new Pen(cP, 6);
       b = new SolidBrush(cB);
       im = Image.FromFile(path);
       f = new Font(new FontFamily("Times New Roman"), 10);
   }
   static void Main() 
   {
       Application.Run(new Altamira1());
   }
   protected override void OnPaint(PaintEventArgs pea)
   {
       Sketch();
       //SketchDBuf();
   }
   private void Sketch()
   {
       Graphics g = Graphics.FromHwnd(this.Handle);  // <=> g = CreateGraphics();
       g.FillRectangle(b, 4, 4, 260, 220);  // passe-partout
       g.DrawRectangle(p, 4, 4, 260, 220);  // frame
       g.DrawImage(im, 33, 35, 200, 145 );  // image
       g.DrawString("ALTAMIRA", f, bT, 180, 190);  // text
       g.Dispose();
   }
   private void SketchDBuf()
   {
       int hh = 3, w = 260, h = 220;   
       Graphics g;
       Bitmap bm = new Bitmap(w + 2*hh, h + 2*hh);
       g = Graphics.FromImage(bm);  // buffer graphics
       g.FillRectangle(b, hh , hh, w, h);  // passe-partout
       g.DrawRectangle(new Pen(Color.Gray,  2*hh), hh, hh, w, h);  // frame
       g.DrawImage(im, hh + 30, hh + 32, 200, 145);  // image
       g.DrawString("ALTAMIRA", f, bT, 180, 190);  // text
       g = Graphics.FromHwnd(this.Handle);  // real graphics
       g.DrawImage(bm, 1, 1);
       g.Dispose();
   }

} }


 </source>


Image.FromStream

<source lang="csharp"> using System; using System.Net; using System.IO; using System.Drawing; using System.Windows.Forms; public class MainClass {

   public static void Main() {
       WebRequest requestPic = WebRequest.Create("http://www.your.ru/1.jpg");
       WebRequest requestHtml = WebRequest.Create("http://www.your.ru");
       WebResponse responsePic = requestPic.GetResponse();
       WebResponse responseHtml = requestHtml.GetResponse();
       Image img = Image.FromStream(responsePic.GetResponseStream());
       using (StreamReader r = new StreamReader(responseHtml.GetResponseStream())) {
           Console.WriteLine(r.ReadToEnd());
       }
   }

}

 </source>


Image.GetThumbnailImage

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;

class Thumbnail: Form {

    const int iSquare = 64;
    Image imageThumbnail;
  
    public static void Main()
    {
         Application.Run(new Thumbnail());
    }
    public Thumbnail()
    {
         Text = "Thumbnail";
         ResizeRedraw = true; 
         Image image = Image.FromFile("Color.jpg");
  
         int cxThumbnail, cyThumbnail;
  
         if (image.Width > image.Height)
         {
              cxThumbnail = iSquare;
              cyThumbnail = iSquare * image.Height / image.Width;
         }
         else
         {
              cyThumbnail = iSquare;
              cxThumbnail = iSquare * image.Width / image.Height;
         }
         imageThumbnail = image.GetThumbnailImage(cxThumbnail, cyThumbnail,
                                                  null, (IntPtr) 0);
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }      
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         for (int y = 0; y < cy; y += iSquare){
             for (int x = 0; x < cx; x += iSquare)
                  grfx.DrawImage(imageThumbnail, 
                                 x + (iSquare - imageThumbnail.Width) / 2,
                                 y + (iSquare - imageThumbnail.Height) / 2,
                                 imageThumbnail.Width, imageThumbnail.Height);
         }
    }

}

 </source>


Image.Height

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;

class CenterPixelSizeImage: Form {

    Image image = Image.FromFile("Color.jpg");
  
    public static void Main()
    {
         Application.Run(new CenterPixelSizeImage());
    }
    public CenterPixelSizeImage()
    {
         ResizeRedraw = true; 
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         grfx.DrawImage(image, (cx - image.Width)  / 2,
                               (cy - image.Height) / 2,
                               image.Width, image.Height);
    }

}

 </source>


Image.RotateFlip(RotateFlipType.Rotate180FlipY)

<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,

  Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information ISBN: 1861007663

  • /

using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace ImageFlip {

   /// <summary>
   /// Summary description for ImageFlip.
   /// </summary>
   public class ImageFlip : System.Windows.Forms.Form
   {
       private System.Windows.Forms.GroupBox groupBox1;
       private System.Windows.Forms.CheckBox checkBox1;
       private System.Windows.Forms.RadioButton radioButton1;
       Image im = null;
       Image im2 = null;
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.RadioButton radioButton2;
       private System.Windows.Forms.RadioButton radioButton3;
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ruponentModel.Container components = null;
       public ImageFlip()
       {
           //
           // Required for Windows Form Designer support
           //
           InitializeComponent();
           this.Text = "Flipping and Rotating";
           this.radioButton1.Checked = false;
           this.label1.Text = "";
           this.groupBox1.Text = "RotateFlipType";
           this.checkBox1.Text = "Paint";
           this.radioButton1.Text = "Rotate180FlipY";
           this.radioButton2.Text = "Rotate180FlipX";
           this.radioButton3.Text = "Rotate180FlipNone";
           this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
           this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
           this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
           //
           // TODO: Add any constructor code after InitializeComponent call
           //
       }
       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       protected override void Dispose( bool disposing )
       {
           if( disposing )
           {
               if (components != null) 
               {
                   components.Dispose();
               }
           }
           base.Dispose( disposing );
       }
       #region Windows Form Designer generated code
       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           this.groupBox1 = new System.Windows.Forms.GroupBox();
           this.radioButton1 = new System.Windows.Forms.RadioButton();
           this.checkBox1 = new System.Windows.Forms.CheckBox();
           this.label1 = new System.Windows.Forms.Label();
           this.radioButton2 = new System.Windows.Forms.RadioButton();
           this.radioButton3 = new System.Windows.Forms.RadioButton();
           this.groupBox1.SuspendLayout();
           this.SuspendLayout();
           // 
           // groupBox1
           // 
           this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                   this.radioButton3,
                                                                                   this.radioButton2,
                                                                                   this.radioButton1,
                                                                                   this.checkBox1});
           this.groupBox1.Location = new System.Drawing.Point(312, 64);
           this.groupBox1.Name = "groupBox1";
           this.groupBox1.Size = new System.Drawing.Size(248, 80);
           this.groupBox1.TabIndex = 1;
           this.groupBox1.TabStop = false;
           this.groupBox1.Text = "groupBox1";
           // 
           // radioButton1
           // 
           this.radioButton1.Location = new System.Drawing.Point(120, 16);
           this.radioButton1.Name = "radioButton1";
           this.radioButton1.Size = new System.Drawing.Size(112, 24);
           this.radioButton1.TabIndex = 1;
           this.radioButton1.Text = "radioButton1";
           // 
           // checkBox1
           // 
           this.checkBox1.Location = new System.Drawing.Point(16, 16);
           this.checkBox1.Name = "checkBox1";
           this.checkBox1.TabIndex = 0;
           this.checkBox1.Text = "checkBox1";
           this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
           // 
           // label1
           // 
           this.label1.Location = new System.Drawing.Point(8, 8);
           this.label1.Name = "label1";
           this.label1.Size = new System.Drawing.Size(304, 200);
           this.label1.TabIndex = 2;
           this.label1.Text = "label1";
           // 
           // radioButton2
           // 
           this.radioButton2.Location = new System.Drawing.Point(16, 48);
           this.radioButton2.Name = "radioButton2";
           this.radioButton2.TabIndex = 2;
           this.radioButton2.Text = "radioButton2";
           // 
           // radioButton3
           // 
           this.radioButton3.Location = new System.Drawing.Point(120, 48);
           this.radioButton3.Name = "radioButton3";
           this.radioButton3.Size = new System.Drawing.Size(120, 24);
           this.radioButton3.TabIndex = 3;
           this.radioButton3.Text = "radioButton3";
           // 
           // ImageFlip
           // 
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(560, 214);
           this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                         this.label1,
                                                                         this.groupBox1});
           this.Name = "ImageFlip";
           this.Text = "ImageFlip";
           this.groupBox1.ResumeLayout(false);
           this.ResumeLayout(false);
       }
       #endregion
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main() 
       {
           Application.Run(new ImageFlip());
       }
       protected override void OnPaint(PaintEventArgs e)
       {   RotateFlip();   }
       private void label1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
       {   RotateFlip();   }
       private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
       {   RotateFlip();   }
       private void radioButtons_CheckedChanged(object sender, System.EventArgs e)
       {   RotateFlip();   }
       protected void RotateFlip()
       {
           Graphics g = Graphics.FromHwnd(this.label1.Handle);
           Brush b = new SolidBrush(this.label1.BackColor);
   
           if (this.checkBox1.Checked) 
           {
               if (im == null) ReadImage();
               Graphics g2 = Graphics.FromImage(im);  // For text "Himalaya"
               FontFamily ff = new FontFamily("Times New Roman");
               Font f = new Font(ff, 25, FontStyle.Bold);
               g2.DrawString("HIMALAYA", f, new SolidBrush(Color.Yellow), 170, 210);
               g2.Dispose();               
               im2 = (Image)im.Clone();
           
               int w2 = label1.Width/2, h2 = label1.Height/2;
               g.DrawImage(im, 0, 0, w2, h2);
       
               if(this.radioButton1.Checked)  // Rotate180FlipY
               {
                   im2.RotateFlip(RotateFlipType.Rotate180FlipY);
                   g.DrawImage(im2, w2, 0, w2, h2);
               }
               else g.FillRectangle(b, w2, 0, w2, h2);  // Clear old
               if(this.radioButton2.Checked)  // Rotate180FlipX
               {
                   im2.RotateFlip(RotateFlipType.Rotate180FlipX);
                   g.DrawImage(im2, 0, h2, w2, h2);
               }
               else g.FillRectangle(b, 0, h2, w2, h2);  // Clear old
       
               if(this.radioButton3.Checked)  // Rotate180FlipNone
               {
                   im2.RotateFlip(RotateFlipType.Rotate180FlipNone);
                   g.DrawImage(im2, w2, h2, w2, h2);  // Clear old
               }
               else g.FillRectangle(b, w2, h2, w2, h2);
               im2.Dispose();
           }
           else Clear(g);
   
           b.Dispose();    g.Dispose();
       }
       protected void ReadImage()
       {
           string path = @"Himalaya.bmp";
           im = Image.FromFile(path);
           this.radioButton1.Enabled = true;
           this.radioButton2.Enabled = true;
           this.radioButton3.Enabled = true;
       }
       protected void Clear(Graphics g)
       {
           g.Clear(this.BackColor);
           g.Dispose();        
           im = null;
           im2 = null;
           this.radioButton1.Checked = false;
           this.radioButton2.Checked = false;
           this.radioButton3.Checked = false;
           this.radioButton1.Enabled = false;
           this.radioButton2.Enabled = false;
           this.radioButton3.Enabled = false;
       }
   }

}


 </source>


Image.VerticalResolution

<source lang="csharp">

using System; using System.Drawing; using System.Windows.Forms;

class CenterImage: Form {

    Image image = Image.FromFile("Color.jpg");
  
    public static void Main()
    {
         Application.Run(new CenterImage());
    }
    public CenterImage()
    {
         ResizeRedraw = true; 
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         grfx.PageUnit = GraphicsUnit.Pixel;
         grfx.PageScale = 1;
  
         RectangleF rectf = grfx.VisibleClipBounds;
  
         float cxImage = grfx.DpiX * image.Width / 
                                            image.HorizontalResolution;
         float cyImage = grfx.DpiY * image.Height / 
                                            image.VerticalResolution;
  
         grfx.DrawImage(image, (rectf.Width  - cxImage) / 2, 
                               (rectf.Height - cyImage) / 2);
    }

}

 </source>