ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 IEvolution: GetNetImage and transparency
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

andrejzr

2 Posts

Posted - Sep 02 2014 :  04:22:09  Show Profile  Reply
I am using GetNetImage call to get a System.Drawing.Image from IEImage (IEvolution).

If the loaded image is PNG with transparency, the resulting Image does not contain Alpha Channel.

Looking at the Format of the IEImage, it is ie24RBG. So it makes sense. But setting it to ie32RGB and then calling SynchronizeRGBA (like suggested in another post) does not work. After setting the Format to ie32RGB, the format is still 24 bit, and calling SynchronizeRGBA after that causes Access violation.

Workarround is to save IEImage to memory stream and load Image from the same stream. But I am afraid that this would affect the performance.

Do you have any other hints how to get a .NET image with alpha channel?

Thanks
Andrej

xequte

38616 Posts

Posted - Sep 15 2014 :  22:46:36  Show Profile  Reply
The current version of IEvolution cannot transfer transparency to .net Image object.

You can try the following workaround:

ieViewer1.Image.LoadImage(“test.png");
pictureBox1.Image = CreateImageWithTransparency(ieViewer1.Image);

Where CreateImageWithTransparency is defined as:

    public static Image CreateImageWithTransparency(IEImage image)
    {
      const int bytesPerPixel = 4;
      Image originalImage = image.GetNetImage();
      if ((originalImage.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)
      {
        return originalImage;
      }
      Bitmap bmp = new Bitmap(originalImage);
      PixelFormat pxf = PixelFormat.Format32bppArgb;
      Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
      BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf);
      IntPtr ptr = bmpData.Scan0;
      int numBytes = bmp.Width * bmp.Height * bytesPerPixel;
      byte[] argbValues = new byte[numBytes];
      System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, numBytes);
      for (int counter = 0, x = 0, y = 0; counter < argbValues.Length; counter += bytesPerPixel)
      {
        int pos = 0;
        pos++; // B value
        pos++; // G value
        pos++; // R value
        argbValues[counter + pos] = (byte)image.GetAlpha(x, y);
        if (++x == bmp.Width)
        {
          x = 0;
          ++y;
        }
      }
      System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, numBytes);
      bmp.UnlockBits(bmpData);
      return bmp;
    }


Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: