hi,
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);
if (bmp != null) {
frmSettings.SetImage((int)udMonth.Value-1, bmp);
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));
}
public static Image resizeImage(Image image, int newwidth, int newheight, RotateFlipType rotateFlipType)
{
Image rotatedImage = (Image)image.Clone();
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);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
after resize
ImageAttributes attribute = new ImageAttributes();
attribute.SetWrapMode(WrapMode.TileFlipXY);
Rectangle rect = new Rectangle(new Point(0, 0), newSize);
graphics.DrawImage(rotatedImage, rect, 0, 0, rotatedImage.Width,
rotatedImage.Height, GraphicsUnit.Pixel, attribute);
return resizedImage;
}
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();
}
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);
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