|
Since you're specifying the source DC (dcMemory), I would try passing values for arguments 3,4 & 6 (i.e., the various size and positions). Argument 6 should just be the address of a POINT with value (0,0).
I use this function all the time, and that's what I do (returns non zero - and it works of course )
|
|
|
|
|
I have a 32 bit bitmap that I want to convert to a 1 bit monochrome bitmap but the problem is that it has all it's alpha bits set to 0xFF. This results in the monochrome bitmap being all black. Is there an easy way, short of the GetDIBits()-traverse-SetDIBits() route, to set all the alpha bits to 0x00?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Having an alpha of FF is the correct starting point - something else is going wrong. An alpha of FF is fully opaque; if you set the alphas to all 00, the resulting bitmap would be 100% transparent.
|
|
|
|
|
Then my problem must be elsewhere.
pja::CBitmap bmp1(IDB_BITMAP1);
pja::CBitmap bmp2(IDB_BITMAP2);
pja::CBitmap bmpbw = CreateBitmap(bmp1.Width(), bmp1.Height(), 1, 1, NULL);
ShowGraphic(bmp1);
ShowGraphic(bmp2);
pja::CCompatibleDC dc1;
pja::CCompatibleDC dc2;
pja::CCompatibleDC dcbw;
SelectObject(dc1, bmp1);
SelectObject(dc2, bmp2);
SelectObject(dcbw, bmpbw);
BitBlt(dc1, 0, 0, bmp1.Width(), bmp1.Height(), dc2, 0, 0, 0x00990066);
ShowGraphic(dc1);
SetBkColor(dc1, RGB(255, 255, 255));
BitBlt(dcbw, 0, 0, bmpbw.Width(), bmpbw.Height(), dc1, 0, 0, SRCCOPY);
ShowGraphic(bmpbw);
The final step does not work, the b&w bitmap is all black. If I change my display settings to 16 or 24 bit then this code works perfect. The only time it has a problem is with 32 bits, that is why I suspected the alpha bits as being the cause.
So Ok, same question but opposite. How to ensure all alpha bits are 0xFF if they are 0x00?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Nope, I was not mistaken. Changing the alpha bits from 0xFF to 0x00 solved the problem.
But I still would like to know if there is an easier way than the GetDIBits()-traverse-SetDIBits() method that I have used so far.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
The solution to set all the alpha bits to zero is actually quite simple (now that I know it). It involves a white brush, the PatBlt function, and the DPa ROP.
SelectObject (dc1, GetStockObject(WHITE_BRUSH));
PatBlt (dc1, 0, 0, bmp1.Width(), bmp1.Height(), 0x00A000C9);
Now that is a lot simplier code than going through all the steps required for the GetDIBits() /SetDIBits() method I have been using.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Hi,
I am not sure but this section seems the most suitable one. I want to clear a partial area of a bitmap using graphics object in c#. I also mean making it transparent by "clear". Is it possible? How?
What i am trying to do is; I am drawing a rectangle on a transparent bitmap and displaying it in the picturebox. There is another image at the background of picturebox. And I want to highlight and move that rectangle without clearing whole image and redrawing it. Any ideas?
Thanks for any comments and directings..
oktay
|
|
|
|
|
Could you use a layered approach?
Have your picture box at the back,
the bitmap above that,
then draw a repeated black texture on top of it all.
This is assuming I got it right, that you want the rectangle to cover up some part of a bitmap which displays over some sort of semi-visible background.
Yet another spam post on yet another forum!
I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to.
I love being a student.
|
|
|
|
|
how can i change forms caption and borders?
i wanna have caption in new color and shape with custom title and position and new color for borders
many skin designers dont support rtl layout and i wanna design one myself
|
|
|
|
|
|
Does anyone know of any libraries for reading JPEG Extended files ( which I believe are 12 bit ), preferably in .NET ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hey Christian,
Do you have a sample image I can borrow?
Mark
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
I am not sure. The images are being passed to me inside a DICOM image. I don't know for sure if the tag is correct in all cases, some of hte images I have tagged as JPEG Extended, may be plain old JPEGs.
I can pull some out and look for one that GDI+ doesn't open.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
OK, I have one, where do I send it ? Sending a private message to me on CP won't work right now, send it instead to christian dot graus, that's my gmail address.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
Richard A. Abbott wrote: Don't know how deep your pockets are
These are the clowns I am trying to avoid. I've had no good dealings with them, and they are ridiculous in their pricing.
Richard A. Abbott wrote: but you might find this interesting reading
Thanks, I will check it out.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Help! I'm working on a control that does a fair amount of work to render itself. I've got it set up where it basically does all of its drawing to a bitmap that it holds on to, and then in the Paint method just copies the data from the bitmap to the client area, depdning on how much of it is currently visible, zoom factor, etc. Certain operations by the user can cause this bitmap to need to be redrawn. This is a time consuming operation (might take 5-10 seconds), and I'd like to update the bitmap in a worker thread, so that the app doesn't freeze while its doing its rendering. Anyone know how to go about this?
I created a BackgroundWorker, as follows:
I initialize the worker here:
worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.WorkerSupportsCancellation = false;
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
Then, if my bitmap needs to be redrawn, I start it here:
worker.RunWorkerAsync();
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// Create a new bitmap for the log
int width = (int)Math.Round(Width * fDPIX);
int height = LogHeightPixels;
bmp = new Bitmap(width, height);
DrawLogToBitmap(bmp);
e.Result = bmp;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Bitmap bmpNew = (Bitmap)e.Result;
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
bmp = bmpNew;
}
In the DoWork handler, I create a new bitmap, do the drawing, and then assign the bitmap to the Result parameter in the RunWorkerCompletedEventArgs. It looks fine here, but then I get back to the main thread (in the RunWorkerCompleted) and the bitmap looks like garbage there.
Any ideas what I might be doing wrong?
Thanks In Advance,
Aaron
|
|
|
|
|
What language is that code in?
If the "bmp" created in worker_DoWork is the same one disposed of in worker_RunWorkerCompleted,
and it's the same one passed in e.Result, then doesn't
Bitmap bmpNew = (Bitmap)e.Result; <-- isn't bmpNew == bmp here?
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
bmp = bmpNew;
}
dispose the bitmap before you use it?
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
You're right, I wan't paying attention there. Updated code looks like this:
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int width = (int)Math.Round(Width * fDPIX);
int height = LogHeightPixels;
Bitmap bmpNew = new Bitmap(width, height/*, System.Drawing.Imaging.PixelFormat.Gdi/*.Format32bppArgb*/);
DrawLogToBitmap(bmpNew);
e.Result = bmpNew;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Bitmap bmpNew = (Bitmap)e.Result;
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
bmp = bmpNew;
Refresh();
}
Refresh ends up calling my Draw method here:
public override void Draw(Graphics gfx, Rectangle rect)
{
gfx.ScaleTransform(zoom, zoom);
// Update the bitmap (only as needed)
if (bDirty)
{
UpdateBitmap(gfx);
}
if (bmp == null || worker.IsBusy)
{
gfx.DrawString("Updating Bitmap, please wait!", this.Font, Brushes.Black, new PointF(0, 0));
return;
}
RectangleF rcSource = new RectangleF(-horizontalOffset, -verticalOffset, ClientRectangle.Width/zoom, ClientRectangle.Height/zoom);
RectangleF rcDest = new RectangleF(ClientRectangle.Location.X, ClientRectangle.Location.Y, ClientRectangle.Size.Width/zoom, ClientRectangle.Size.Height/zoom);
try
{
gfx.DrawImage(bmp, rcDest, rcSource, GraphicsUnit.Pixel);
}
catch (Exception exc)
{
gfx.DrawString(exc.Message, Font, Brushes.Black, new PointF(0,0));
}
etc....
}
Now, the bitmap seems OK, but I get an out of memory exception when I call DrawImage (hence the try/catch block). Using the exact same drawing code in the main thread causes no problems, even though it's a pretty big bitmap (2496 * 49152 * 32 bpp). If I try using a bitmapp with a different pixel format, say 16bpp to conserve memory, performance becomes terrible, but I don't get the exception.
|
|
|
|
|
I've never done C#....what thread does the worker_RunWorkerCompleted() get executed on?
Thanks,
Mark
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
That one runs on the main thread. The handler for the DoWork event runs in the worker thread.
Aaron
|
|
|
|
|
Hmm then with a (close to) 500MB image maybe the thread adds just enough to put memory use
over the top?
When you do this:
Bitmap bmpNew = (Bitmap)e.Result;
That just copies a reference, right? It doesn't make a new duplicate object?
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
Since your Draw method makes sure that while the background thread is busy, a draw doesn't happen on the bitmap - you don't need to keep creating a new bitmap. In fact, looking at the size of the bitmap you're creating, I bet a lot of your 5-10 second draw is taken up by simply creating that bitmap object. This is of course making the assumption that your bitmap size isn't constantly changing - if that is the case, then ignore this post - if the bitmap size is (fairly) constant, then keep reading.
class SomeForm : Form
{
private Bitmap _theImage;
private bool _working = false;
void worker_DoWork(object sender, DoWorkEventArgs e)
{
if (_theImage == null)
{
int width = (int)Math.Round(Width * fDPIX);
int height = LogHeightPixels;
_theImage = new Bitmap(width, height);
}
DrawLogToBitmap(_theImage);
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_working = false;
Refresh();
}
public override void Draw(Graphics gfx, Rectangle rect)
{
gfx.ScaleTransform(zoom, zoom);
if (bDirty)
{
_working = true;
UpdateBitmap(gfx);
}
if (_working || worker.IsBusy)
{
gfx.DrawString("Updating Bitmap, please wait!", this.Font, Brushes.Black, new PointF(0, 0));
return;
}
RectangleF rcSource = new RectangleF(-horizontalOffset, -verticalOffset, ClientRectangle.Width/zoom, ClientRectangle.Height/zoom);
RectangleF rcDest = new RectangleF(ClientRectangle.Location.X, ClientRectangle.Location.Y, ClientRectangle.Size.Width/zoom, ClientRectangle.Size.Height/zoom);
try
{
gfx.DrawImage(_theImage, rcDest, rcSource, GraphicsUnit.Pixel);
}
catch (Exception exc)
{
gfx.DrawString(exc.Message, Font, Brushes.Black, new PointF(0,0));
}
etc....
}
}
I may have missed something (I've been up for FAR too long, and can't sleep for some reason), but that's the gist of it. You'll probably want to do a few more checks to make sure the bitmap isn't being accessed from multiple threads at the same time. This should get rid of your memory exception, and give a significant performance boost, since the garbage collector won't need to run as often and you're not allocating massive amounts of memory all the time.
Another thing you may want to get rid of is that background worker, and just use a normal thread with a longer lifetime. Spawning a new thread for each request does add a bit of overhead, so in similar situations I typically start up a background thread early in the program, and use an AutoResetEvent or similar to tell it when to wake up and do something.
-----
In the land of the blind, the one eyed man is king.
|
|
|
|
|
hi,
i am working on a project that deals with tiff files. i am using a picture box to display the image from the tiff file, but i can only display the first image. how can i display other images of the same tiff file?
regards
|
|
|
|
|
don't worry, i found the answer.
regards
|
|
|
|
|