问题描述
hi
如果我为
选择图像,则通过按钮单击事件将图像上传到图像控件中第一次会正确显示.如果我再次选择,它将显示错误,例如:
GDI +中发生了一般性错误,这是我的代码:
hi
i uploading the image in the image control with button click event if i select the image for the
first time it will dispalyed correctly .if i reselect again it will display error like:
A generic error occurred in GDI+ here is my code:
private void button11_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
string fname = null;
ofd.Filter = "JPEG|*.jpg|GIF|*.gif|BMP|*.bmp|ICO|*.ico";
ofd.Title = "Select Picture File";
ofd.Multiselect = false ;
ofd.AddExtension = false;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fname = ofd.FileName;
Bitmap originalBMP = new Bitmap(fname);
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 100;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
string filename = null;
filename = "baby10.bmp";
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
oGraphics.Dispose();
originalBMP.Dispose();
// Save the new graphic file to the server
newBMP.Save(("D:\\") + filename);
newBMP.Dispose();
FileStream fs = new FileStream("D:\\baby10.bmp", FileMode.Open, FileAccess.Read);
img = new byte[fs.Length + 1];
fs.Read(img, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
image1.Source = new BitmapImage(new Uri("D:\\baby10.bmp"));
flag = true;
fs.Dispose();
}
}
如果有人有任何想法在此编码中做错了什么,请纠正我并提出建议
在此先感谢
[edit]已添加代码块-OriginalGriff [/edit]
if anyone have anyidea what iam doing wrong in this coding please correct me and give suggestion
thanks in advance
[edit]Code block added - OriginalGriff[/edit]
推荐答案
img = new byte[fs.Length + 1];
为什么在字节数组上+1?这意味着您的图像数组在图像数据的末尾将有一个额外的字节0.
Why +1 on the byte array? It means your image array will have an extra byte 0 at the end of the image data.
这篇关于GDI +中发生一般错误.如何解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!