[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

Resolution to Smooth TIFF B&W Image

Jorge Paredes

12/29/2004 11:25:00 PM

Hi eveyone,
just if any of you have the same problem.

Some days ago I post the problem that I had.

I have B&W TIFF Group 4 Images that I need to display
with some smooth effect, many of you have seen the "Scale to gray"
option of some applications like "Imaging for Windows" or something like
that.
I need that functionality in my program.

I'm using some GDI interop, StretchBlt, to display the Bitmap on the
screen, I'm
using GDI because I need high performance on the display of the image, I
tested a thousand
times GDI+ vs GDI, including many suggestions that I see on some forums but
always GDI
gives me more speed at the time of scrolling, zooming, repainting the
ClipRectangle, in my case
there's no doubt, GDI is best for speed than GDI+.

In most forums I get the following code to display the Bitmap so quickly:
IntPtr pTarget=e.Graphics.GetHdc();
IntPtr pSource=Win32Support.CreateCompatibleDC(pTarget);
IntPtr pOrig=Win32Support.SelectObject(pSource,myBMP.GetHBitmap());

Win32Support.StretchBlt(pTarget,clipX,clipY,clipW,clipH,pSource,scaledX,scal
edY,scaledW,scaledH,Win32Support.TernaryRasterOperations.MERGECOPY);

IntPtr pNew=Win32Support.SelectObject(pSource,pOrig);
Win32Support.DeleteObject(pNew);
Win32Support.DeleteDC(pSource);
e.Graphics.ReleaseHdc(pTarget);

This was more quick than Graphics.DrawImage when repainting all,
nevertheless for repainting only the ClipRectangle
it is so slow, the key is myBMP.GetHBitmap(), it is the bottleneck, as I
read somewhere GetHBitmap creates a GDI
bitmap with the data of the GDI+ bitmap and returns the pointer.
What I'm doing now is use GetHBitmap when I load the image, then I use the
pointer around the process and when I do not
need the image I DeleteObject() the bitmap pointer, or when I load another
Image I DeleteObject too.

This is the base of the performance of my application.
Now, with all of this I get a Image a little ugly without smoothing, just
like is stored in the file pixel by pixel.

What I'm doing is convert from 1bpp to 32bpp and my images get a cool
smooth, it takes almost no time and it's
done just when I load the image or when I change the Page.

if(fs!=null) fs.Close();
fs=new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
imgView=Image.FromStream(fs);

Bitmap bmTmp=new
Bitmap(imgView.Width,imgView.Height,PixelFormat.Format32bppArgb);

Graphics grTmp=Graphics.FromImage(bmTmp);

grTmp.InterpolationMode=InterpolationMode.High;
grTmp.DrawImageUnscaled(imgView,0,0);

if(bmScaled!=null) bmScaled.Dispose();
bmScaled=(Bitmap)bmTmp.Clone();
grTmp.Dispose();
bmTmp.Dispose();
if(memBMP!=IntPtr.Zero) Win32Support.DeleteObject(memBMP);
memBMP=bmScaled.GetHbitmap();

The code on top is what I'm using to load the image.
First, I'm using Image.FromStream, not Image.FromFile...test it
yourself....FromStream is more quick,
I don't know why.

I'm creating a new bitmap with Format32bppArgb, then I created a Graphics
derived from the 32bpp Bitmap
and I'm using InterpolationMode.High to do the smooth thing.
I draw over the Graphics the original Image that I loaded, then put the
result 32bpp bitmap in my working Bitmap
and dispose the temporal Bitmap and Graphics objects. I get the Bitmap
pointer only once....memBMP is the
pointer that I'm using in StretchBlt instead of using GetHBitmap in the
SelectObject preparation.

I just want to share this with you because it tooks me a lot of time and I
don't see anything on the internet that
can help me with exactly what I need.

I can't post all the code because is propietary but I think this post can
help somebody.

Jorge Paredes