當前位置: 首頁>>代碼示例>>C#>>正文


C# Bitmap.Save方法代碼示例

本文整理匯總了C#中System.Drawing.Bitmap.Save方法的典型用法代碼示例。如果您正苦於以下問題:C# Bitmap.Save方法的具體用法?C# Bitmap.Save怎麽用?C# Bitmap.Save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了Bitmap.Save方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: RegisterCommands

        private void RegisterCommands(ImagePresentationViewModel viewModel)
        {
            viewModel.SaveCommand = UICommand.Regular(() =>
            {
                var dialog = new SaveFileDialog
                {
                    Filter = "PNG Image|*.png|Bitmap Image|*.bmp",
                    InitialDirectory = Settings.Instance.DefaultPath
                };
                var dialogResult = dialog.ShowDialog();
                if (dialogResult.HasValue && dialogResult.Value)
                {
                    var tmp = viewModel.Image;
                    using (var bmp = new Bitmap(tmp))
                    {
                        if (File.Exists(dialog.FileName))
                        {
                            File.Delete(dialog.FileName);
                        }

                        switch (dialog.FilterIndex)
                        {
                            case 0:
                                bmp.Save(dialog.FileName, ImageFormat.Png);
                                break;
                            case 1:
                                bmp.Save(dialog.FileName, ImageFormat.Bmp);
                                break;
                        }
                    }
                }
            });
        }
開發者ID:PictoCrypt,項目名稱:ImageTools,代碼行數:33,代碼來源:ImagePresentationController.cs

示例2: TakeSnapshot

 private void TakeSnapshot()
 {
     var stamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm");
     Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
     Graphics.FromImage(bmp).CopyFromScreen(0, 0, 0, 0, bmp.Size);
     bmp.Save(@"C:\Temp\" + stamp + ".jpg", ImageFormat.Jpeg);
 }
開發者ID:FrontFabric,項目名稱:Link,代碼行數:7,代碼來源:FrmMain.cs

示例3: CaptureScreen

 /// <summary>
 /// Capture the screen and saves as image
 /// </summary>
 /// <returns> The creted image filename</returns>
 public string CaptureScreen()
 {
     date = DateTime.Now.ToString("ddMMyyyy");
     string fileName = DateTime.Now.ToString("hhmmss");
     int ix, iy, iw, ih;
     ix = Convert.ToInt32(Screen.PrimaryScreen.Bounds.X);
     iy = Convert.ToInt32(Screen.PrimaryScreen.Bounds.Y);
     iw = Convert.ToInt32(Screen.PrimaryScreen.Bounds.Width);
     ih = Convert.ToInt32(Screen.PrimaryScreen.Bounds.Height);
     Bitmap image = new Bitmap(iw, ih,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);
     Graphics g = Graphics.FromImage(image);
     g.CopyFromScreen(ix, iy, ix, iy,
              new System.Drawing.Size(iw, ih),
              CopyPixelOperation.SourceCopy);
     try
     {
         if (Directory.Exists(GlobalContants.screenshotFolderPath))
             image.Save(Path.Combine(GlobalContants.screenshotFolderPath, fileName + ".jpeg"), ImageFormat.Jpeg);
         else
         {
             Directory.CreateDirectory(GlobalContants.screenshotFolderPath);
             image.Save(Path.Combine(GlobalContants.screenshotFolderPath, fileName + ".jpeg"), ImageFormat.Jpeg);
         }
     }
     catch (Exception) { }
     return fileName;
 }
開發者ID:niv7488,項目名稱:final-project---class-board,代碼行數:32,代碼來源:ImageCaptureManager.cs

示例4: button2_Click

 private void button2_Click(object sender, EventArgs e)
 {
     Bitmap[] bimp = new Bitmap[openFileDialog1.SafeFileNames.Length];
     for (int i = 0; i < bimp.Length; i++)
     {
         bimp[i] = new Bitmap(openFileDialog1.FileNames[i]);
     }
     Bitmap resutl = new Bitmap(bimp[0].Width * bimp.Length, bimp[1].Height);
     Graphics q = Graphics.FromImage(resutl);
      //   q.ScaleTransform(0.5f, 0.5f);
     for (int i = 0; i < bimp.Length; i++)
     {
         q.DrawImage((Image)bimp[i], new Point(bimp[i].Width * i, 0));
     }
     q.Flush();
     if ((!checkBox1.Checked) && (saveFileDialog1.FileName != null))
     {
         resutl.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
     }
     else
     {
         resutl.Save(openFileDialog1.FileNames[0] + "RESULT.png", System.Drawing.Imaging.ImageFormat.Png);
     }
     MessageBox.Show("Готово");
 }
開發者ID:Spyman,項目名稱:Game,代碼行數:25,代碼來源:Form1.cs

示例5: GetImageStream

        private static void GetImageStream( ImageFormat outputFormat, Bitmap bitmap, MemoryStream ms )
        {
            var imageEncoders = ImageCodecInfo.GetImageEncoders ();
            var encoderParameters = new EncoderParameters (1);
            encoderParameters.Param[0] = new EncoderParameter (Encoder.Quality, 100L);

            if (ImageFormat.Jpeg.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[1], encoderParameters);
            }
            else if (ImageFormat.Png.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[4], encoderParameters);
            }
            else if (ImageFormat.Gif.Equals (outputFormat))
            {
                var quantizer = new OctreeQuantizer (255, 8);
                using (var quantized = quantizer.Quantize (bitmap))
                {
                    quantized.Save (ms, imageEncoders[2], encoderParameters);
                }
            }
            else if (ImageFormat.Bmp.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[0], encoderParameters);
            }
            else
            {
                bitmap.Save (ms, outputFormat);
            }
        }
開發者ID:andriybilas,項目名稱:MasterM,代碼行數:31,代碼來源:ScalableImageUtility.cs

示例6: timer1_Tick

 private void timer1_Tick(object sender, EventArgs e)
 {
     if (Clipboard.ContainsImage() & System.IO.Directory.Exists(textBox1.Text))
     {
         frameNr++;
         Bitmap scr = new Bitmap(Clipboard.GetImage());
         if (checkBox1.Checked)
         {
             Graphics g = Graphics.FromImage(scr);
             Rectangle re = new Rectangle();
             re.X = MousePosition.X - 10;
             re.Y = MousePosition.Y - 10;
             re.Size = Cursor.Size;
             Cursor.Draw(g, re);
         }
         if (!System.IO.Directory.Exists(textBox1.Text + "\\Pictures"))
             System.IO.Directory.CreateDirectory(textBox1.Text + "\\Pictures");
         string fn = frameNr.ToString();
         while (fn.Length < 3)
             fn = "0" + fn;
         if (radioButton3.Checked)
             scr.Save(textBox1.Text + "\\Pictures\\" + fn + radioButton3.Text, System.Drawing.Imaging.ImageFormat.Bmp);
         else if (radioButton1.Checked)
             scr.Save(textBox1.Text + "\\Pictures\\" + fn + radioButton1.Text, System.Drawing.Imaging.ImageFormat.Jpeg);
         else if (radioButton2.Checked)
             scr.Save(textBox1.Text + "\\Pictures\\" + fn + radioButton2.Text, System.Drawing.Imaging.ImageFormat.Png);
         Clipboard.Clear();
         Application.DoEvents();
     }
 }
開發者ID:d-kakhiani,項目名稱:C_Sharp,代碼行數:30,代碼來源:Form1.cs

示例7: saveImage2File

 private void saveImage2File(Bitmap screenshot, string fileName)
 {
     if (String.IsNullOrEmpty(fileName))
         return;
     string ext = Path.GetExtension(fileName);
     switch (ext.ToLower())
     {
         case ".png":
             screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
             break;
         case ".jpg":
         case ".jpeg":
             screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
             break;
         case ".tiff":
             screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
             break;
         case ".bmp":
             screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
             break;
         case ".gif":
             screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
             break;
     }
 }
開發者ID:TownSuite,項目名稱:cobra,代碼行數:25,代碼來源:Screenshot.cs

示例8: BmpSave

        public static void BmpSave(Bitmap newBmp, string outFile)
        {
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 100;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;

            //獲得包含有關內置圖像編碼解碼器的信息的ImageCodecInfo 對象。
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x];//設置JPEG編碼
                    break;
                }
            }

            if (jpegICI != null)
            {
                newBmp.Save(outFile, jpegICI, encoderParams);
            }
            else
            {
                newBmp.Save(outFile, ImageFormat.Jpeg);
            }
            newBmp.Dispose();
        }
開發者ID:rew170,項目名稱:soomecode,代碼行數:31,代碼來源:ImageHandle.cs

示例9: switch

        void iDS.Save(Bitmap b, string fileName)
        {
            var formatFile = fileName;
            formatFile = formatFile.Substring(formatFile.Length - 3);

            switch (formatFile)
            {
                case "bmp":
                    b.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case "jepg":
                case "jpg":
                    b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "png":
                    b.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                case "gif":
                    b.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                case "tiff":
                    b.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
                    break;
            }           
        }
開發者ID:Igor-Rashyn,項目名稱:exercises,代碼行數:25,代碼來源:DS_Standard.cs

示例10: Form1_Load

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmpImage = new Bitmap("sagarmatha.jpg");
            pictureBox1.Image = new Bitmap("sagarmatha.jpg");
            int width = bmpImage.Width;
            int height = bmpImage.Height;
            for (int j = 0; j< height; j++)
            {
                for (int i = 0; i< width; i++)
                {
                    Color pixel1 = bmpImage.GetPixel(i, j);
                    Color newcolor = Color.FromArgb(pixel1.A, pixel1.R, 0, 0);
                    bmpImage.SetPixel(i, j, newcolor);
                }

                bmpImage.Save("output_redscale.jpg");
                }
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    Color pixel1 = bmpImage.GetPixel(i, j);
                    int average = (pixel1.R + pixel1.G + pixel1.B) / 3;
                    Color newcolor = Color.FromArgb(0, average, average, average);
                    bmpImage.SetPixel(i, j, newcolor);
                }
                bmpImage.Save("output_grayscale.jpg");
                }
            pictureBox2.Image = new Bitmap("output_redscale.jpg");
            pictureBox3.Image = new Bitmap("output_grayscale.jpg");
        }
開發者ID:sadipgiri,項目名稱:Sadip1,代碼行數:31,代碼來源:Form1.cs

示例11: Main

        static void Main(string[] args)
        {
            FileSize("noname.jpg");
            var image = new Bitmap("noname.jpg");

            var jgpEncoder = GetEncoder(ImageFormat.Jpeg);
            var myEncoder = Encoder.Quality;

            var myEncoderParameters = new EncoderParameters(1);
            var myEncoderParameter = new EncoderParameter(myEncoder, 50L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            image.Save(@"TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);
            FileSize("TestPhotoQualityFifty.jpg");

            myEncoderParameter = new EncoderParameter(myEncoder, 100L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            image.Save(@"TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);
            FileSize("TestPhotoQualityHundred.jpg");

            myEncoderParameter = new EncoderParameter(myEncoder, 70L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            image.Save(@"TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);
            FileSize("TestPhotoQualityZero.jpg");

            Console.ReadLine();
        }
開發者ID:kurniawirawan,項目名稱:ImageCompression,代碼行數:26,代碼來源:Program.cs

示例12: LoadImage

        private static void LoadImage(HttpPostedFileBase file, string filename, string folder)
        {
            string largePhotoFullName = Path.Combine(HttpContext.Current.Server.MapPath("~/Content"),
                                                  folder, "FullSize", file.FileName);
            string smallPhotoFullName = Path.Combine(HttpContext.Current.Server.MapPath("~/Content"),
                                                    folder, "Small", file.FileName);

            if (!String.IsNullOrEmpty(file.FileName))
            {
                file.SaveAs(largePhotoFullName);
                try
                {
                    //изменяем размер изображения и перезаписываем на диск
                    var imageLogo = System.Drawing.Image.FromFile(largePhotoFullName);
                    var bitmapLogo = new Bitmap(imageLogo);
                    imageLogo.Dispose();
                    int newWidth = 500;
                    bitmapLogo = Resize(bitmapLogo, newWidth, bitmapLogo.Height * newWidth / bitmapLogo.Width);
                    bitmapLogo.Save(largePhotoFullName);
                    newWidth = 100;
                    bitmapLogo = Resize(bitmapLogo, newWidth, bitmapLogo.Height * newWidth / bitmapLogo.Width);
                    bitmapLogo.Save(smallPhotoFullName);

                    //пересохраняем с нужным нам названием
                    File.Move(largePhotoFullName, HttpContext.Current.Server.MapPath("~/Content/" + folder + "/FullSize/") + filename);
                    File.Move(smallPhotoFullName, HttpContext.Current.Server.MapPath("~/Content/" + folder + "/Small/") + filename);
                }
                catch //пользователь загрузил не изображение (ну или другая ошибка)
                {
                    File.Delete(largePhotoFullName);
                    File.Delete(smallPhotoFullName);
                }
            }
        }
開發者ID:JIy3AHKO,項目名稱:MvcApplication1,代碼行數:34,代碼來源:ImageHelper.cs

示例13: SizeScaling

 public Image SizeScaling(Bitmap image, int width, int height, bool distorted)
 {
     try
     {
         double realRatio = ((double) image.Height)/((double) image.Width);
         if (width != 0 & height != 0)
         {
             if (distorted) return image.GetThumbnailImage(width, height, ThumbnailCallback, IntPtr.Zero);
             var heightAfterRatio = (int) Math.Floor(realRatio*width);
             var widthAfterRatio = (int) Math.Floor(height/realRatio);
             using (var mem1 = new MemoryStream())
             {
                 using (var mem2 = new MemoryStream())
                 {
                     image.Save(mem1, ImageFormat.Tiff);
                     ImageBuilder.Current.Build(new ImageJob(mem1, mem2,
                         new ResizeSettings(widthAfterRatio, heightAfterRatio, FitMode.None, null)));
                     return Image.FromStream(mem2);
                 }
             }
             //return image.GetThumbnailImage(widthAfterRatio, heightAfterRatio, ThumbnailCallback, IntPtr.Zero);
         }
         if (width != 0)
         {
             var heightAfterRatio = (int) Math.Floor(realRatio*width);
             using (var mem1 = new MemoryStream())
             {
                 using (var mem2 = new MemoryStream())
                 {
                     image.Save(mem1, ImageFormat.Jpeg);
                     ImageBuilder.Current.Build(new ImageJob(mem1, mem2,
                         new ResizeSettings(width, heightAfterRatio, FitMode.None, null)));
                     return Image.FromStream(mem2);
                 }
             }
             //return image.GetThumbnailImage(width, heightAfterRatio, ThumbnailCallback, IntPtr.Zero);
         }
         if (height != 0)
         {
             var widthAfterRatio = (int) Math.Floor(height/realRatio);
             using (var mem1 = new MemoryStream())
             {
                 using (var mem2 = new MemoryStream())
                 {
                     image.Save(mem1, ImageFormat.Jpeg);
                     ImageBuilder.Current.Build(new ImageJob(mem1, mem2,
                         new ResizeSettings(widthAfterRatio, height, FitMode.None, null)));
                     return Image.FromStream(mem2);
                 }
             }
             //return image.GetThumbnailImage(widthAfterRatio, height, ThumbnailCallback, IntPtr.Zero);
         }
         return image;
     }
     catch (Exception ex)
     {
         return image;
     }
 }
開發者ID:CheViana,項目名稱:ImageServer,代碼行數:59,代碼來源:ScalingProcessor.cs

示例14: GenThumbnail

 public static void GenThumbnail(Image imageFrom, string pathImageTo, int width, int height,string fileExt)
 {
     if (imageFrom == null)
     {
         return;
     }
     // 源圖寬度及高度
     int imageFromWidth = imageFrom.Width;
     int imageFromHeight = imageFrom.Height;
     // 生成的縮略圖實際寬度及高度
     int bitmapWidth = width;
     int bitmapHeight = height;
     // 生成的縮略圖在上述"畫布"上的位置
     int X = 0;
     int Y = 0;
     // 根據源圖及欲生成的縮略圖尺寸,計算縮略圖的實際尺寸及其在"畫布"上的位置
     if (bitmapHeight * imageFromWidth > bitmapWidth * imageFromHeight)
     {
         bitmapHeight = imageFromHeight * width / imageFromWidth;
         Y = (height - bitmapHeight) / 2;
     }
     else
     {
         bitmapWidth = imageFromWidth * height / imageFromHeight;
         X = (width - bitmapWidth) / 2;
     }
     // 創建畫布
     Bitmap bmp = new Bitmap(width, height);
     Graphics g = Graphics.FromImage(bmp);
     // 用白色清空
     g.Clear(Color.White);
     // 指定高質量的雙三次插值法。執行預篩選以確保高質量的收縮。此模式可產生質量最高的轉換圖像。
     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
     // 指定高質量、低速度呈現。
     g.SmoothingMode = SmoothingMode.HighQuality;
     // 在指定位置並且按指定大小繪製指定的 Image 的指定部分。
     g.DrawImage(imageFrom, new Rectangle(X, Y, bitmapWidth, bitmapHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);
     try
     {
         //經測試 .jpg 格式縮略圖大小與質量等最優
         if (fileExt == ".png")
         {
             bmp.Save(pathImageTo, ImageFormat.Png);
         }
         else
         {
             bmp.Save(pathImageTo, ImageFormat.Jpeg);
         }
     }
     catch
     {
     }
     finally
     {
         //顯示釋放資源
         bmp.Dispose();
         g.Dispose();
     }
 }
開發者ID:XingsiStudio,項目名稱:ArtGallery,代碼行數:59,代碼來源:UploadFileHandler.ashx.cs

示例15: CreateVerifyImg

        /// <summary>
        /// 生成驗證碼圖片
        /// </summary>
        private void CreateVerifyImg()
        {
            string vMapPath = string.Empty;                     //驗證碼路徑
            Random rnd = new Random();                          //隨機發生器
            Bitmap imgTemp = null;                              //驗證碼圖片
            Graphics g = null;                                  //圖形庫函數
            SolidBrush blueBrush = null;                        //緩衝區

            //暫時注銷
            //string rndstr = rnd.Next(1111, 9999).ToString();    //驗證碼

            string rndstr = string.Empty;    //驗證碼
            //測試時,將驗證碼統一設為0000
            rndstr = rnd.Next(1111, 9999).ToString();

            int totalwidth = 0;                                 //驗證碼圖片總寬度
            int totalheight = 0;                                //驗證碼圖片總高度

            //給緩存添加驗證碼
            SetVerifyCookie(rndstr);

            //生成驗證碼圖片文件
            System.Drawing.Image ReducedImage1 = System.Drawing.Image.FromFile(Server.MapPath("images/yzm/" + rndstr.Substring(0, 1) + ".gif"));
            System.Drawing.Image ReducedImage2 = System.Drawing.Image.FromFile(Server.MapPath("images/yzm/" + rndstr.Substring(1, 1) + ".gif"));
            System.Drawing.Image ReducedImage3 = System.Drawing.Image.FromFile(Server.MapPath("images/yzm/" + rndstr.Substring(2, 1) + ".gif"));
            System.Drawing.Image ReducedImage4 = System.Drawing.Image.FromFile(Server.MapPath("images/yzm/" + rndstr.Substring(3, 1) + ".gif"));

            totalwidth = 52;
            totalheight = 17;
            imgTemp = new Bitmap(totalwidth, totalheight);
            g = Graphics.FromImage(imgTemp);
            blueBrush = new SolidBrush(Color.Black);
            g.FillRectangle(blueBrush, 0, 0, totalwidth, totalheight);
            g.DrawImage(ReducedImage1, 0, 0);
            g.DrawImage(ReducedImage2, ReducedImage1.Width, 0);
            g.DrawImage(ReducedImage3, ReducedImage1.Width + ReducedImage2.Width, 0);
            g.DrawImage(ReducedImage4, ReducedImage1.Width + ReducedImage2.Width + ReducedImage3.Width, 0);

            vMapPath = Server.MapPath("images/yzm/verify.gif");
            try
            {
                imgTemp.Save(vMapPath);
            }
            catch
            {
                //utils.JsAlert("創建驗證驗錯誤!");
            }

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            imgTemp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());

            blueBrush.Dispose();
            g.Dispose();
            imgTemp.Dispose();
        }
開發者ID:scutsky,項目名稱:iyuewan,代碼行數:61,代碼來源:verifyimg.aspx.cs


注:本文中的System.Drawing.Bitmap.Save方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。