// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace crudwork.Utilities { /// <summary> /// Image Utility /// </summary> public static class ImageUtil { #region Enumerators #endregion #region Fields private static int thumbWidth = 96; private static int thumbHeight = 96; #endregion #region Constructors #endregion #region Event Methods #region System Event Methods #endregion #region Application Event Methods #endregion #region Custom Event Methods #endregion #endregion #region Public Methods /// <summary> /// Create thumbnail from an image filename. /// </summary> /// <param name="filename"></param> /// <returns></returns> public static Image GetThumbnail(string filename) { return GetThumbnail(Image.FromFile(filename), thumbWidth, thumbHeight); } /// <summary> /// Create thumbnail with the given size from an image filename. /// </summary> /// <param name="filename"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Image GetThumbnail(string filename, int width, int height) { return GetThumbnail(Image.FromFile(filename), width, height); } /// <summary> /// Create thumbnail from an Image type. /// </summary> /// <param name="image"></param> /// <returns></returns> public static Image GetThumbnail(Image image) { return GetThumbnail(image, thumbWidth, thumbHeight); } /// <summary> /// Create thumbnail with the given size for an Image type. /// </summary> /// <param name="image"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Image GetThumbnail(Image image, int width, int height) { if (image == null) throw new ArgumentNullException("image"); return image.GetThumbnailImage(width, height, null, new IntPtr()); } /// <summary> /// Read the binary content from a file into a byte array. /// </summary> /// <param name="filename"></param> /// <returns></returns> public static byte[] FileToByteArray(string filename) { using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { byte[] imageData = new Byte[fs.Length]; fs.Read(imageData, 0, imageData.Length); fs.Close(); return imageData; } } /// <summary> /// Convert a byte array to Image type. /// </summary> /// <param name="value"></param> /// <returns></returns> public static Image ByteArrayToImage(byte[] value) { if ((value == null) || (value.Length == 0)) throw new ArgumentNullException("value"); return Image.FromStream(new MemoryStream(value)); } /// <summary> /// Create a thumbnail image from an image filename. /// </summary> /// <param name="filename"></param> /// <returns></returns> public static byte[] CreateThumbnail(string filename) { using (MemoryStream s = new MemoryStream()) using (Image image = Image.FromFile(filename).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr())) { image.Save(s, ImageFormat.Jpeg); return s.ToArray(); } } /// <summary> /// Create a thumbnail image from a byte array, presumely from an image. /// </summary> /// <param name="imageData"></param> /// <returns></returns> public static byte[] CreateThumbnail(byte[] imageData) { using (MemoryStream s = new MemoryStream()) using (Image image = Image.FromStream(new MemoryStream(imageData)).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr())) { image.Save(s, ImageFormat.Jpeg); return s.ToArray(); } } /// <summary> /// Convert an image to a byte array /// </summary> /// <param name="imageToConvert"></param> /// <param name="formatOfImage"></param> /// <returns></returns> public static byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage) { try { using (MemoryStream ms = new MemoryStream()) { imageToConvert.Save(ms, formatOfImage); return ms.ToArray(); } } catch (Exception ex) { throw; } } /// <summary> /// Return the ImageFormat type based on the file's extension /// </summary> /// <param name="filename"></param> /// <returns></returns> public static ImageFormat GetImageFormat(string filename) { string ext = new FileInfo(filename).Extension.Substring(1); switch (ext.ToUpper()) { case "BMP": return ImageFormat.Bmp; //case "": // return ImageFormat.Emf; //case "": // return ImageFormat.Exif; case "GIF": return ImageFormat.Gif; case "ICO": return ImageFormat.Icon; case "JPEG": case "JPG": case "JPE": return ImageFormat.Jpeg; //case "": // return ImageFormat.MemoryBmp; case "PNG": return ImageFormat.Png; case "TIF": case "TIFF": return ImageFormat.Tiff; case "WMF": return ImageFormat.Wmf; default: return null; } } #endregion #region Private Methods #endregion #region Protected Methods #endregion #region Properties #endregion #region Others #endregion } }
Image Zoom
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1= new System.Windows.Forms.Label(); private System.Windows.Forms.GroupBox groupBox1= new System.Windows.Forms.GroupBox(); private System.Windows.Forms.RadioButton radioButton1= new System.Windows.Forms.RadioButton(); private System.Windows.Forms.RadioButton radioButton2= new System.Windows.Forms.RadioButton(); private System.Windows.Forms.RadioButton radioButton3= new System.Windows.Forms.RadioButton(); private System.Windows.Forms.RadioButton radioButton4= new System.Windows.Forms.RadioButton(); private System.Windows.Forms.Label label2= new System.Windows.Forms.Label(); private System.Windows.Forms.CheckBox checkBox1= new System.Windows.Forms.CheckBox(); Image im = null; Image im2 = null; public Form1() { this.groupBox1.SuspendLayout(); this.SuspendLayout(); this.label1.Location = new System.Drawing.Point(8, 16); this.label1.Size = new System.Drawing.Size(200, 240); this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.checkBox1, this.radioButton1, this.radioButton2, this.radioButton3, this.radioButton4}); this.groupBox1.Location = new System.Drawing.Point(232, 48); this.groupBox1.Size = new System.Drawing.Size(72, 128); this.checkBox1.Location = new System.Drawing.Point(8, 32); this.checkBox1.Size = new System.Drawing.Size(56, 24); this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); this.radioButton1.Location = new System.Drawing.Point(8, 64); this.radioButton1.Size = new System.Drawing.Size(16, 24); this.radioButton2.Location = new System.Drawing.Point(40, 64); this.radioButton2.Size = new System.Drawing.Size(16, 24); this.radioButton3.Location = new System.Drawing.Point(8, 96); this.radioButton3.Size = new System.Drawing.Size(16, 24); this.radioButton4.Location = new System.Drawing.Point(40, 96); this.radioButton4.Size = new System.Drawing.Size(16, 24); this.label2.Location = new System.Drawing.Point(328, 16); this.label2.Size = new System.Drawing.Size(200, 240); this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(536, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox1, this.label1, this.label2}); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); this.Text = "Zooming"; this.label1.Text = ""; this.groupBox1.Text = "Zoom"; this.checkBox1.Text = "Paint"; this.radioButton1.Checked = false; 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); this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged); } [STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(PaintEventArgs e) { ImageZoom(); } private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); } private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); } protected void ImageZoom() { Graphics g1 = Graphics.FromHwnd(this.label1.Handle); Graphics g2 = Graphics.FromHwnd(this.label2.Handle); Rectangle rec; Rectangle recPart; if (this.checkBox1.Checked) { if (im == null) ReadImage(); rec = new Rectangle(0, 0, label1.Width, label1.Height); g1.DrawImage(im, rec); recPart = new Rectangle(im.Width / 4, im.Height / 4, im.Width / 2, im.Height / 2); if (this.radioButton1.Checked) recPart = new Rectangle(0, 0, im.Width / 2, im.Height / 2); if (this.radioButton2.Checked) recPart = new Rectangle(im.Width / 2, 0, im.Width / 2, im.Height / 2); if (this.radioButton3.Checked) recPart = new Rectangle(0, im.Height / 2, im.Width / 2, im.Height / 2); if (this.radioButton4.Checked) recPart = new Rectangle(im.Width / 2, im.Height / 2, im.Width / 2, im.Height / 2); g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel); } else { Clear(g1); Clear(g2); } g1.Dispose(); g2.Dispose(); } protected void ReadImage() { string path = "3.BMP"; im = Image.FromFile(path); this.radioButton1.Enabled = true; this.radioButton2.Enabled = true; this.radioButton3.Enabled = true; this.radioButton4.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.radioButton4.Checked = false; this.radioButton1.Enabled = false; this.radioButton2.Enabled = false; this.radioButton3.Enabled = false; this.radioButton4.Enabled = false; } }
Image.GetThumbnailImage
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.IO; public class Form1 : System.Windows.Forms.Form { [STAThread] static void Main() { Application.Run(new Form1()); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { string p = "s.JPG"; Image i = Image.FromFile(p); Image tn = i.GetThumbnailImage(50, 50, null, IntPtr.Zero); // <=>(IntPtr)0 e.Graphics.DrawImage(tn, 100, 0, tn.Width, tn.Height); e.Graphics.Dispose(); } }
Image Flipping and Rotating
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : 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; public Form1() { 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(); 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.Size = new System.Drawing.Size(248, 80); this.radioButton1.Location = new System.Drawing.Point(120, 16); this.radioButton1.Size = new System.Drawing.Size(112, 24); this.checkBox1.Location = new System.Drawing.Point(16, 16); this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Size = new System.Drawing.Size(304, 200); this.radioButton2.Location = new System.Drawing.Point(16, 48); this.radioButton3.Location = new System.Drawing.Point(120, 48); this.radioButton3.Size = new System.Drawing.Size(120, 24); 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.groupBox1.ResumeLayout(false); this.ResumeLayout(false); 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); } [STAThread] static void Main() { Application.Run(new Form1()); } 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); 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){ im2.RotateFlip(RotateFlipType.Rotate180FlipY); g.DrawImage(im2, w2, 0, w2, h2); } else g.FillRectangle(b, w2, 0, w2, h2); if (this.radioButton2.Checked) { im2.RotateFlip(RotateFlipType.Rotate180FlipX); g.DrawImage(im2, 0, h2, w2, h2); } else g.FillRectangle(b, 0, h2, w2, h2); if (this.radioButton3.Checked) { im2.RotateFlip(RotateFlipType.Rotate180FlipNone); g.DrawImage(im2, w2, h2, w2, h2); } else g.FillRectangle(b, w2, h2, w2, h2); im2.Dispose(); } else Clear(g); b.Dispose(); g.Dispose(); } protected void ReadImage() { string path = "a.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; } }
Scribble with Bitmap
using System; using System.Drawing; using System.Windows.Forms; class ScribbleWithBitmap: Form { bool bTracking; Point ptLast; Bitmap bitmap; Graphics grfxBm; public static void Main() { Application.Run(new ScribbleWithBitmap()); } public ScribbleWithBitmap() { Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize; bitmap = new Bitmap(size.Width, size.Height); grfxBm = Graphics.FromImage(bitmap); grfxBm.Clear(BackColor); } protected override void OnMouseDown(MouseEventArgs mea) { if (mea.Button != MouseButtons.Left) return; ptLast = new Point(mea.X, mea.Y); bTracking = true; } protected override void OnMouseMove(MouseEventArgs mea) { if (!bTracking) return; Point ptNew = new Point(mea.X, mea.Y); Pen pen = new Pen(ForeColor); Graphics grfx = CreateGraphics(); grfx.DrawLine(pen, ptLast, ptNew); grfx.Dispose(); grfxBm.DrawLine(pen, ptLast, ptNew); ptLast = ptNew; } protected override void OnMouseUp(MouseEventArgs mea) { bTracking = false; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height); } }
Load image from a URL(Web) and draw it
using System; using System.Drawing; using System.IO; using System.Net; using System.Windows.Forms; class ImageFromWeb: Form { Image image; public static void Main() { Application.Run(new ImageFromWeb()); } public ImageFromWeb() { ResizeRedraw = true; WebRequest webreq = WebRequest.Create("http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/1_uuid-04f7b0dd-dbeb-4b4c-b943-2f5f92486379_crc-2185242360.jpg"); WebResponse webres = webreq.GetResponse(); Stream stream = webres.GetResponseStream(); image = Image.FromStream(stream); stream.Close(); } 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, 0, 0); } }
Center an Image (VerticalResolution, HorizontalResolution)
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); } }