C\#그림 형식 변환 인 스 턴 스 코드

일상적인 업무 에서 서로 다른 형식의 그림 이 필요 하고 가끔 은 그림 형식의 상호 전환 이 필요 하 다.본 고 는 간단 한 작은 예 로 그림 형식 전환 의 흔 한 방법 을 약술 하고 공유 하 는 것 만 배 울 수 있 으 며 부족 한 점 이 있 으 면 지적 해 주 십시오.
지식 에 관련되다
  • OpenFileDialog 파일 대화 상 자 를 열 고 파일 을 선택 할 때 필터 접 두 사 를 설정 할 수 있 습 니 다
  • FolderBrowserDialog 폴 더 선택 대화 상 자 는 폴 더 를 선택 할 때 추가 할 수 있 습 니 다
  • ImageFormat 그림 형식 매 거..
  • Bitmap 비트 맵 대상 은 해당 하 는 속성 과 내용 을 포함 합 니 다
  • Stream 흐름 대상 의 기본 클래스.
  • FlowLayoutPanel 스 트림 레이아웃 용기 에 추 가 된 요 소 는 가로 또는 세로 로 순서대로 배열 합 니 다
  • 예제 효과 도
    그림 변환기 의 예제 효과 도 는 다음 과 같다.

    핵심 코드
    그림 열기
    
    /// <summary>
        ///     
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
    
          this.fileDialog.Filter = fileFilter;
          this.fileDialog.Multiselect = true;
          this.fileDialog.CheckFileExists = true;
          if (fileDialog.ShowDialog() == DialogResult.OK)
          {
            string[] fileNames = this.fileDialog.FileNames;
            foreach(string fileName in fileNames)
            {
              Bitmap bmp = new Bitmap(fileName);
              //      
              bmp.Tag = Path.GetFileNameWithoutExtension(fileName);
              PictureBox box = new PictureBox();
              box.Image = bmp;
              box.Width = 105;
              box.Height = 150;
              box.BorderStyle = BorderStyle.FixedSingle;
              box.Padding = new Padding(2);
              this.flowPnl.Controls.Add(box);
            }
            this.txtFile.Text = Path.GetDirectoryName(fileNames[0]);
    
          }
        }
    그림 형식 변환
    
    /// <summary>
        ///     
        /// </summary>
        private void convertImage(string dir, string filter,Bitmap bmp)
        {
          string filePath = Path.Combine(dir, string.Format("{0}.{1}", bmp.Tag.ToString(), filter.ToLower()));
          switch (filter)
          {
            case "JPG":
              bmp.Save(filePath, ImageFormat.Jpeg);
              break;
            case "PNG":
              bmp.Save(filePath, ImageFormat.Png);
              break;
            case "GIF":
              bmp.Save(filePath, ImageFormat.Gif);
              break;
            case "BMP":
              bmp.Save(filePath, ImageFormat.Bmp);
              break;
            case "ICO":
              Stream stream = File.Create(filePath);
              Icon icon = Icon.FromHandle(bmp.GetHicon());
              icon.Save(stream);    //  save the icon
              stream.Close();
              break;
          }
        }
    예제 의 소스 코드 가 필요 하 다 면 링크 를 클릭 하여 다운로드 할 수 있 습 니 다.
    소스 코드 링크
    이상 은 C\#그림 형식 변환 의 인 스 턴 스 코드 에 대한 상세 한 내용 입 니 다.c\#그림 형식 변환 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!

    좋은 웹페이지 즐겨찾기