C# 手动/自动保存图片

简介:

view plaincopy to clipboardprint?
//手动保存图片   
        private void saveBtn_Click(object sender, System.EventArgs e)   
        {   
            bool isSave = true;   
            SaveFileDialog saveImageDialog = new SaveFileDialog();   
            saveImageDialog.Title = "图片保存";   
            saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";   
  
            if(saveImageDialog.ShowDialog() == DialogResult.OK)   
            {   
                string fileName = saveImageDialog.FileName.ToString();   
        
                if(fileName != "" && fileName != null)   
                {   
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();   
  
                    System.Drawing.Imaging.ImageFormat imgformat = null;        
    
                    if(fileExtName!="")   
                    {   
                        switch(fileExtName)    
                        {    
                            case "jpg":    
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;    
                                break;    
                            case "bmp":    
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;    
                                break;    
                            case "gif":    
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;    
                                break;    
                            default:    
                                MessageBox.Show("只能存取为: jpg,bmp,gif 格式");    
                                isSave = false;   
                                break;    
                        }    
  
                    }   
  
                    //默认保存为JPG格式   
                    if(imgformat == null)   
                    {   
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;   
                    }   
                       
                    if(isSave)   
                    {   
                        try  
                        {   
                                this.pictureBox1.Image.Save(fileName,imgformat);   
                                //MessageBox.Show("图片已经成功保存!");   
                        }   
                        catch  
                        {   
                            MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");   
                        }   
                    }   
  
                }   
  
            }   
        }   
  
        //自动保存图片   
        private void Autosave()   
        {   
            string Opath =@"D:\VedioCapture\Photo";   
            string photoname = DateTime.Now.Ticks.ToString();   
            if (Opath.Substring(Opath.Length-1, 1) != @"\")   
                Opath = Opath + @"\";   
            string path1 = Opath + DateTime.Now.ToShortDateString();   
            if (! Directory.Exists(path1))             
                Directory.CreateDirectory(path1);   
            //pictureBox1.Image.Save(path1 +"\\" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);   
            //图像的缩小   
            System.Drawing.Bitmap objPic,objNewPic;   
            try  
            {   
                objPic = new System.Drawing.Bitmap(pictureBox1.Image);   
                objNewPic=new System.Drawing.Bitmap(objPic,pictureBoxShow.Width,pictureBoxShow.Height);   
                //objNewPic=new System.Drawing.Bitmap(objPic,320,240);//图片保存的大小尺寸   
                objNewPic.Save(path1 +"\\" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);   
            }   
            catch(Exception exp){throw exp;}   
            finally  
            {   
                objPic=null;   
                objNewPic=null;   
            }   
        } 

分类:  ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2013/03/18/2966255.html ,如需转载请自行联系原作者
相关文章
|
测试技术 C# Windows
C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试
原文:C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试 WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 。 XAML CS Attempt 1: ImageMy_Image=newImage(Openfile.
1765 0
|
23天前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
257 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
4月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
6月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
21 0
|
9月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
12月前
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
118 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
592 0
|
C# 图形学
C#裁剪图片的方法
C#裁剪图片的方法
253 0
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
952 0
如何在 C#中的listView 控件中显示图片?