使用位图的问题 [英] problem with using bitmap
问题描述
使用位图时遇到问题。
这就是我所做的:
从文件加载位图
在类型字典集合中存储位图(frmSettings.SetImage(...))
got a problem working with bitmaps.
that's what i do :
load bitmap from file
store bitmap in collection of type dictionary (frmSettings.SetImage(...))
// *****************************************************************************
private void OpenFile(string sFile)
// *****************************************************************************
{
Bitmap bmp;
try {
bmp = (Bitmap)Image.FromFile(sFile, false); // load bitmap from file
if (bmp != null) {
frmSettings.SetImage((int)udMonth.Value-1, bmp);
// Keep bitmap in class member
m_Bitmap = (Bitmap)bmp.Clone();
pnlImage.Invalidate();
}
}
catch (Exception ex) {
MessageBox.Show(this, ex.Message, "Error loading from file");
}
}
在面板上绘制位图,用户可以在表格上的任何位置放置
,因为面板可以任意大小,位图大小调整
draw bitmap on a panel which the user can place anywhere on the form
since panel could be of any size, bitmap is resized
// *****************************************************************************
private void pnlImage_Paint(object sender,PaintEventArgs e)
// *****************************************************************************
{
if ( m_Bitmap != null ) {
Bitmap bmp = (Bitmap)UCCalender.resizeImage(m_Bitmap, nlImage.Width,
pnlImage.Height);
e.Graphics.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
}
}
// ******************************************************************************
public static Image resizeImage(Image image, int newwidth, int newheight)
// ******************************************************************************
{
return(resizeImage(image,newwidth,newheight, RotateFlipType.RotateNoneFlipNone));
}
/// <summary>
/// Resizes and rotates an image, keeping the original aspect ratio. Does not dispose the original Image instance.
/// </summary>
/// <param name="image">Image instance</param>
/// <param name="width">desired width</param>
/// <param name="height">desired height</param>
/// <param name="rotateFlipType">desired RotateFlipType</param>
/// <returns>new resized/rotated Image instance</returns>
// *****************************************************************************
public static Image resizeImage(Image image, int newwidth, int newheight, RotateFlipType rotateFlipType)
// *****************************************************************************
{
// clone the Image instance, since we don't want to resize the original Image instance
Image rotatedImage = (Image)image.Clone(); // as Image;
rotatedImage.RotateFlip(rotateFlipType);
Size newSize = CalculateResizedDimensions(rotatedImage, newwidth, newheight);
Bitmap resizedImage = new Bitmap(newSize.Width, newSize.Height,
PixelFormat.Format32bppArgb);
resizedImage.SetResolution(72, 72);
Graphics graphics = Graphics.FromImage(resizedImage);
// set parameters to create a high-quality thumbnail
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// use an image attribute in order to remove the black/gray border around image
after resize
// (most obvious on white images), see this post for more information:
// http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
//using (var attribute = new ImageAttributes())
ImageAttributes attribute = new ImageAttributes();
attribute.SetWrapMode(WrapMode.TileFlipXY);
// draws the resized image to the bitmap
Rectangle rect = new Rectangle(new Point(0, 0), newSize);
graphics.DrawImage(rotatedImage, rect, 0, 0, rotatedImage.Width,
rotatedImage.Height, GraphicsUnit.Pixel, attribute);
return resizedImage;
}
将位图作为byte []保存到应用程序cfg-file
save bitmap as byte[] to the applications cfg-file
// *****************************************************************************
private static byte[] ImageToByteArraybyMemoryStream(Image image)
// *****************************************************************************
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();
}
正常工作!!!
新应用程序启动我加载位图来自cfg-file
并被添加到词典
THAT WORKS PREFECTLY !!!
on new start of application i load bitmap from cfg-file
and is added to dictionary
// ******************************************************************************
public static Image ByteArrayToImagebyMemoryStream(byte[] bytearray)
// ******************************************************************************
{
MemoryStream ms = new MemoryStream(bytearray);
Image image = Image.FromStream(ms);
return image;
}
m_Bitmap = (Bitmap)frmSettings.GetImage(0);
现在绘制位图,方法RotateFlip(rotateFlipType)抛出
a gdi + - 异常虽然它不会做任何事情,因为参数是RotateFlipType.RotateNoneFlipNone
with try {} catch {}我可以避免这个问题但是这对我来说无法帮助我理解出了什么问题。
为您的帮助提前感谢
franz
now drawing the bitmap, method RotateFlip(rotateFlipType) throws
a gdi+ - exception though it won't do anything since the parameter is RotateFlipType.RotateNoneFlipNone
with try{} catch{} i could avoid this problem but that won't help me
to understand what's going wrong.
for your help thanks in advance
franz
推荐答案
它与流有关。从流中获取位图后,将位图复制到另一个对象。并使用在流上使用
进行正确处理。
It is related to the stream. Copy the bitmap to a different object after you got it from the stream. And use using
on the stream to be properly disposed.
这篇关于使用位图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!