1 显示图片
主要命令:
Image.FromFile();
graphics.DrawImage(image);
Code
protected override void OnPaint(PaintEventArgs paintEvnt)
{
Image newImage = Image.FromFile("C:/test.jpg");
// 设置图像显示的左上角
PointF ulCorner = new PointF(10.0F, 10.0F);
// 显示出图片.
paintEvnt.Graphics.DrawImage(newImage, ulCorner);
}
protected override void OnPaint(PaintEventArgs paintEvnt)
{
Image newImage = Image.FromFile("C:/test.jpg");
// 设置图像显示的左上角
PointF ulCorner = new PointF(10.0F, 10.0F);
// 显示出图片.
paintEvnt.Graphics.DrawImage(newImage, ulCorner);
}
2 图像缩略图
主要命令:
image.GetThumbnailImage();
Code
public void ResizeImage(string OrigFile, string NewFile, int NewWidth, int MaxHeight, bool ResizeIfWider)
{
System.Drawing.Image FullSizeImage = System.Drawing.Image.FromFile(OrigFile);
// Ensure the generated thumbnail is not being used by rotating it 360 degrees
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (ResizeIfWider)
{
if (FullSizeImage.Width <= NewWidth)
{
NewWidth = FullSizeImage.Width;
}
}
int NewHeight = FullSizeImage.Height * NewWidth / FullSizeImage.Width;
if (NewHeight > MaxHeight) // 如果超出了高度限制,则按高度来缩放
{
NewWidth = FullSizeImage.Width * MaxHeight / FullSizeImage.Height;
NewHeight = MaxHeight;
}
// 按照计算的数据进行图像缩放
System.Drawing.Image NewImage = FullSizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
FullSizeImage.Dispose();
NewImage.Save(NewFile);
}
public void ResizeImage(string OrigFile, string NewFile, int NewWidth, int MaxHeight, bool ResizeIfWider)
{
System.Drawing.Image FullSizeImage = System.Drawing.Image.FromFile(OrigFile);
// Ensure the generated thumbnail is not being used by rotating it 360 degrees
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (ResizeIfWider)
{
if (FullSizeImage.Width <= NewWidth)
{
NewWidth = FullSizeImage.Width;
}
}
int NewHeight = FullSizeImage.Height * NewWidth / FullSizeImage.Width;
if (NewHeight > MaxHeight) // 如果超出了高度限制,则按高度来缩放
{
NewWidth = FullSizeImage.Width * MaxHeight / FullSizeImage.Height;
NewHeight = MaxHeight;
}
// 按照计算的数据进行图像缩放
System.Drawing.Image NewImage = FullSizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
FullSizeImage.Dispose();
NewImage.Save(NewFile);
}
3 创建图片文件
主要命令:
Metafile("filename",hdc)。
IntPtr hdc = graphics.GetHdc();
graphics2 = Graphics.FromImage(metaFile);
graphics2.DrawRectangle();
Code
Graphics newGraphics = e.Graphics;
IntPtr hdc = newGraphics.GetHdc();
// 新建一个metafile对象来记录.
Metafile metaFile1 = new Metafile("SampMeta.emf", hdc);
// 在记录文件上创建一个图画板.
Graphics metaGraphics = Graphics.FromImage(metaFile1);
// 画画.
metaGraphics.DrawRectangle(new Pen(Color.Black, 5), 100, 0, 400, 400);
metaGraphics.Dispose();
metaFile1.Dispose();
Graphics newGraphics = e.Graphics;
IntPtr hdc = newGraphics.GetHdc();
// 新建一个metafile对象来记录.
Metafile metaFile1 = new Metafile("SampMeta.emf", hdc);
// 在记录文件上创建一个图画板.
Graphics metaGraphics = Graphics.FromImage(metaFile1);
// 画画.
metaGraphics.DrawRectangle(new Pen(Color.Black, 5), 100, 0, 400, 400);
metaGraphics.Dispose();
metaFile1.Dispose();