Estou a construir a c# contact manager na área de trabalho do aplicativo, onde você pode escolher uma imagem de avatar a partir do ficheiro e depois recortá-lo. A imagem original é exibido corretamente após o corte, para substituir a original. No entanto, mais tarde, ao tentar registrar a conta, a imagem parece ser Nulo com um erro a ser exibida:
Do sistema.ArgumentNullException: "o Valor não pode ser nulo. (Parâmetro 'encoder')"
O método para fazer upload de uma imagem para a página registro:
private void button_browse_Click(object sender, EventArgs e)
{
// select and display image in the picturebox
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Select Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";
if(opf.ShowDialog() == DialogResult.OK)
{
pictureBoxProfileImage.Image = Image.FromFile(opf.FileName);
setImage(pictureBoxProfileImage.Image);
}
}
O método para cortar a imagem:
private void button_Select_Cropped_Area_Click(object sender, EventArgs e)
{
Cursor = Cursors.Default;
Bitmap bitmap2 = new Bitmap(pictureBoxProfileImage.Width, pictureBoxProfileImage.Height);
pictureBoxProfileImage.DrawToBitmap(bitmap2, pictureBoxProfileImage.ClientRectangle);
Bitmap croppedImage = new Bitmap(rectW, rectH);
for (int x = 0; x < rectW; x++)
{
for (int y = 0; y < rectH; y++)
{
Color pxlColor = bitmap2.GetPixel(cropX + x, cropY + y);
croppedImage.SetPixel(x, y, pxlColor);
}
}
pictureBoxProfileImage.Image.Dispose();
pictureBoxProfileImage.Image = (Image)croppedImage;
pictureBoxProfileImage.SizeMode = PictureBoxSizeMode.StretchImage;
}
Aqui está a linha onde o erro ocorre ao salvar a imagem:
MemoryStream picture = new MemoryStream();
pictureBoxProfileImage.Image.Save(picture, pictureBoxProfileImage.Image.RawFormat);
Vale ressaltar que o registro funciona corretamente se a imagem original está sendo passado. Devo substituí-los de alguma forma (substituir o original com a cortada de um)?