Click here to Skip to main content
15,851,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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(...))
C#
// *****************************************************************************
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
C#
// *****************************************************************************
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));
     }
}

C#
// ******************************************************************************
public static Image resizeImage(Image image, int newwidth, int newheight)
// ******************************************************************************
{
  return(resizeImage(image,newwidth,newheight, RotateFlipType.RotateNoneFlipNone));
}

C#
/// <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;
}

save bitmap as byte[] to the applications cfg-file

C#
// *****************************************************************************
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
C#
// ******************************************************************************
public static Image ByteArrayToImagebyMemoryStream(byte[] bytearray)
// ******************************************************************************
{
    MemoryStream ms = new MemoryStream(bytearray);
    Image image = Image.FromStream(ms);
    return image;
}

C#
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
Posted
Comments
Afzaal Ahmad Zeeshan 25-Jan-15 8:32am    
What is the actual exception? Can you please share that line with us?
fheyn 25-Jan-15 10:13am    
it's method RotateFlip(rotateFlipType) (i pointed out that) in method resizeImage().
it only says gdi+ - exeception without any specification.

1 solution

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.
 
Share this answer
 
Comments
fheyn 25-Jan-15 10:20am    
but isn't that what i do when i say
dicImage[i] = ByteArrayToImagebyMemoryStream(ba);
and later i say
m_Bitmap = (Bitmap)frmSettings.GetImage(0);
with GetImage() accessing the dictionary ?
Zoltán Zörgő 25-Jan-15 13:22pm    
What is frmSettings? What is GetImage?
fheyn 26-Jan-15 6:20am    
frmSettings is the form where all the applicationdata can be edited.
frmSettings also saves and loads the applicationdata to the config-file.
I found a bit more precise exception description :
"rotatedImage.Palette" caused "System.Runtime.InteropServices.ExternalException"
ErrorCode = -2147467259

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900