2012-09-19 76 views
2

我在我的WinForm中有一個OpenFileDialog,我想要選擇圖像以限制圖像大小爲100 Ko,尺寸爲350x350。限制圖片大小

我該怎麼做?

+3

你嘗試過什麼?你遇到什麼特定的代碼? –

+0

我對代碼沒有任何問題,我只是不知道如何添加允許我限制圖像大小和尺寸的功能。 –

回答

4
private bool ValidFile(string filename, long limitInBytes, int limitWidth, int limitHeight) 
     { 
      var fileSizeInBytes = new FileInfo(filename).Length; 
      if(fileSizeInBytes > limitInBytes) return false; 

      using(var img = new Bitmap(filename)) 
      { 
       if(img.Width > limitWidth || img.Height > limitHeight) return false; 
      } 

      return true; 
     } 

     private void selectImgButton_Click(object sender, EventArgs e) 
     { 
      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       if(ValidFile(openFileDialog1.FileName, 102400, 350, 350)) 
       { 
        // Image is valid and U can 
        // Do something with image 
        // For example set it to a picture box 
        pictureBox1.Image = new Bitmap(openFileDialog1.FileName); 
       } 
       else 
       { 
        MessageBox.Show("Image is invalid"); 
       } 
      } 
     } 
4

這取決於您需要支持的圖像類型。對於最常見的類型(BMP,JPG,PNG),你可以輕鬆地檢索圖片信息:

string filename = // get it from OpenFileDialog 

if (new FileInfo(filename).Length > SOME_LIMIT) 
{ 
    MessageBox.Show("!!!"); 
} 
else 
{ 
    Image img = Image.FromFile(filename); 
    MessageBox.Show(string.Format("{0} x {1}", img.Width, img.Height)); 
} 

如果您需要多種圖像格式,更廣泛的支持,那麼我建議使用一個圖書館一樣ImageMagick.NET

0

放此作爲全局變量

INT imgSize = 0


private void button1_Click(object sender, EventArgs e) 
{ 
    Image imageFile; 
    OpenFileDialog dlg = new OpenFileDialog(); 

    dlg.Title = "Open Image"; 
    dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*"; 

    if (dlg.ShowDialog() == DialogResult.OK) 
    { 
     imageFile = Image.FromFile(dlg.FileName); 
     imgHeight = imageFile.Height; 
     if (imgHeight > 350) 
     { 
       MessageBox.Show("Not 350x350 Image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
           imgPhoto.Image = null; 
     } 
     else 
     { 
      PictureBox1.Image = new Bitmap(dlg.OpenFile()); 
     } 
     } 
    dlg.Dispose(); 
} 

希望這會有所幫助。

+0

@勺子Yukina,如果我明白你的問題,你想修改圖像爲350x350從原來的大小? – spajce

0

試試這個:

OpenFileDialog fileDialog = new OpenFileDialog 
{ 
    // managed GDI+ supports bmp, jpeg, gif, png and tiff. 
    Filter = 
     "Image files (*.bmp;*.jpg;*.gif;*.png;*.tiff)|*.bmp;*.jpg;*.gif;*.png;*.tiff|All files (*.*)|*.*", 
}; 
if (fileDialog.ShowDialog() == DialogResult.OK) 
{ 
    // Many exceptions could be raised in the following code 
    try 
    { 
     var fileSize = new FileInfo(fileDialog.FileName); 
     var validFilesize = fileSize.Length <= 1024 * 100; // 100 kilo bytes 
     var validDimensions = false; 

     // free the file once we get the dimensions 
     using (Image image = Image.FromFile(fileDialog.FileName)) 
     { 
      validDimensions = (image.Width <= 350) && (image.Height <= 350); 
     } 

     if (!validDimensions || !validFilesize) 
     { 
      MessageBox.Show("Error ! Choose another image"); 
     } 
     else 
     { 
      // do something with the file here 
     } 
    } 
    catch (Exception exception) 
    { 
     MessageBox.Show(exception.Message); 
    } 
}