using System; using System.Drawing; using System.Windows.Forms; class PartialImageRotate: Form { Image image = Image.FromFile("Color.jpg"); public static void Main() { Application.Run(new PartialImageRotate()); } public PartialImageRotate() { 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) { Point[] aptDst = { new Point(0, cy / 2), new Point(cx / 2, 0), new Point(cx / 2, cy) }; Rectangle rectSrc = new Rectangle(95, 5, 50, 55); grfx.DrawImage(image, aptDst, rectSrc, GraphicsUnit.Pixel); } }
Draw text on an Image
using System; using System.Drawing; using System.Windows.Forms; class DrawOnImage: Form { Image image = Image.FromFile("Color.jpg"); string str = "www.kutayzorlu.com/java2s/com"; public static void Main() { Application.Run(new DrawOnImage()); } public DrawOnImage() { ResizeRedraw = true; Graphics grfxImage = Graphics.FromImage(image); grfxImage.PageUnit = GraphicsUnit.Inch; grfxImage.PageScale = 1; SizeF sizef = grfxImage.MeasureString(str, Font); grfxImage.DrawString(str, Font, Brushes.White, grfxImage.VisibleClipBounds.Width - sizef.Width, 0); grfxImage.Dispose(); } 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.DrawImage(image, 0, 0); grfx.DrawString(str, Font, new SolidBrush(clr), grfx.DpiX * image.Width / image.HorizontalResolution, 0); } }
Draw on Pixel-Size Image
using System; using System.Drawing; using System.Windows.Forms; class DrawOnPixelSizeImage: Form { Image image = Image.FromFile("Color.jpg"); string str = "www.kutayzorlu.com/java2s/com"; public static void Main() { Application.Run(new DrawOnPixelSizeImage()); } public DrawOnPixelSizeImage() { ResizeRedraw = true; Graphics grfxImage = Graphics.FromImage(image); Graphics grfxScreen = CreateGraphics(); Font font = new Font(Font.FontFamily, grfxScreen.DpiY / grfxImage.DpiY * Font.SizeInPoints); SizeF sizef = grfxImage.MeasureString(str, font); grfxImage.DrawString(str, font, Brushes.White, image.Width - sizef.Width, 0); grfxImage.Dispose(); grfxScreen.Dispose(); } 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, image.Width, image.Height); grfx.DrawString(str, Font, new SolidBrush(clr), image.Width, 0); } }
Thumbnail Image
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);
}
}
}
[/csharp]
Image Open
using System; using System.Drawing; using System.IO; using System.Windows.Forms; class ImageOpen: Form { protected string strProgName; protected string strFileName; protected Image image; public static void Main() { Application.Run(new ImageOpen()); } public ImageOpen() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&File"); Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...", new EventHandler(MenuFileOpenOnClick), Shortcut.CtrlO)); } void MenuFileOpenOnClick(object obj, EventArgs ea) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" + "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" + "Windows Bitmap (*.bmp)|*.bmp|" + "Windows Icon (*.ico)|*.ico|" + "Graphics Interchange Format (*.gif)|*.gif|" + "JPEG File Interchange Format (*.jpg)|" + "*.jpg;*.jpeg;*.jfif|" + "Portable Network Graphics (*.png)|*.png|" + "Tag Image File Format (*.tif)|*.tif;*.tiff|" + "Windows Metafile (*.wmf)|*.wmf|" + "Enhanced Metafile (*.emf)|*.emf|" + "All Files (*.*)|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { try { image = Image.FromFile(dlg.FileName); } catch (Exception exc) { Console.WriteLine(exc.Message); return; } strFileName = dlg.FileName; Text = Path.GetFileName(strFileName); Invalidate(); } } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; if (image != null) grfx.DrawImage(image, 0, 0); } }
Image Save
using System; using System.Drawing; using System.IO; using System.Windows.Forms; class ImageOpen: Form { protected string strProgName; protected string strFileName; protected Image image; MenuItem miSaveAs; public static void Main() { Application.Run(new ImageOpen()); } public ImageOpen() { ResizeRedraw = true; Menu = new MainMenu(); Menu.MenuItems.Add("&File"); Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...", new EventHandler(MenuFileOpenOnClick), Shortcut.CtrlO)); Menu.MenuItems[0].Popup += new EventHandler(MenuFileOnPopup); miSaveAs = new MenuItem("Save &As..."); miSaveAs.Click += new EventHandler(MenuFileSaveAsOnClick); Menu.MenuItems[0].MenuItems.Add(miSaveAs); } void MenuFileOnPopup(object obj, EventArgs ea) { miSaveAs.Enabled = (image != null); } void MenuFileSaveAsOnClick(object obj, EventArgs ea) { SaveFileDialog savedlg = new SaveFileDialog(); savedlg.InitialDirectory = Path.GetDirectoryName(strFileName); savedlg.FileName = Path.GetFileNameWithoutExtension(strFileName); savedlg.AddExtension = true; savedlg.Filter = "Windows Bitmap (*.bmp)|*.bmp|" + "Graphics Interchange Format (*.gif)|*.gif|" + "JPEG File Interchange Format (*.jpg)|" + "*.jpg;*.jpeg;*.jfif|" + "Portable Network Graphics (*.png)|*.png|" + "Tagged Imaged File Format (*.tif)|*.tif;*.tiff"; if (savedlg.ShowDialog() == DialogResult.OK) { try { image.Save(savedlg.FileName); } catch (Exception exc) { MessageBox.Show(exc.Message, Text); return; } strFileName = savedlg.FileName; Text = strProgName + " - " + Path.GetFileName(strFileName); } } void MenuFileOpenOnClick(object obj, EventArgs ea) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" + "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" + "Windows Bitmap (*.bmp)|*.bmp|" + "Windows Icon (*.ico)|*.ico|" + "Graphics Interchange Format (*.gif)|*.gif|" + "JPEG File Interchange Format (*.jpg)|" + "*.jpg;*.jpeg;*.jfif|" + "Portable Network Graphics (*.png)|*.png|" + "Tag Image File Format (*.tif)|*.tif;*.tiff|" + "Windows Metafile (*.wmf)|*.wmf|" + "Enhanced Metafile (*.emf)|*.emf|" + "All Files (*.*)|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { try { image = Image.FromFile(dlg.FileName); } catch (Exception exc) { Console.WriteLine(exc.Message); return; } strFileName = dlg.FileName; Text = Path.GetFileName(strFileName); Invalidate(); } } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; if (image != null) grfx.DrawImage(image, 0, 0); } }
Fill Ellipse with image based Texture Brush
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.ComponentModel.Container components = null; private Image theImage; private Image smallImage; public Form1() { InitializeComponent(); SetStyle(ControlStyles.Opaque, true); theImage = new Bitmap("Winter.jpg"); smallImage = new Bitmap(theImage,new Size(theImage.Width / 2, theImage.Height / 2)); } protected override void OnPaint(PaintEventArgs e){ Graphics g = e.Graphics; g.FillRectangle(Brushes.White, ClientRectangle); Brush tBrush = new TextureBrush(smallImage, new Rectangle(0, 0,smallImage.Width, smallImage.Height)); g.FillEllipse(tBrush, ClientRectangle); tBrush.Dispose(); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } static void Main() { Application.Run(new Form1()); } }