欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 手动/自动保存图片的实例代码

程序员文章站 2023-12-04 16:17:29
view plaincopy to clipboardprint?复制代码 代码如下: //手动保存图片      &n...

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;  
            }  
        }