C# 图像操作1

1 显示图片

主要命令:

Image.FromFile();

graphics.DrawImage(image);


         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();




        
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();

 


       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();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值