|
stop using the pointer after calling UnlockBits, or postpone calling UnlockBits until you're done with the pointer.
FWIW: When passing a data pointer to some native code, I prefer using the GCHandle class.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thanks so far,
I describe my problem a little bit more exactly:
As soon as my camera has taken a new picture, I get a BYTE- (or char)-pointer of it. This happens in unmanaged code, but it's no problem
to delegate it to managed.
Now I create my bitmap like that:
myBitmapData = myImage->LockBits(*myRect,WriteOnly,Format8bppIndexed);
myBitmapData->Scan0 = myBytePtr;
myBitmapData->Stride = iImageWidth*iImagePlanes;
myImage->UnlockBits(myBitmapData);
The final call of UnlockBits makes my BYTE pointer to an undefined value, so if this Function is called again (and it has to be called again to define the new Bitmap) without a new Image was taken, the function gets an empty pointer and crashes.
Is there no possibility to keep that pointer?
Or would anyone of you even create that bitmap some other way?
I'm not sure if this method is a good one...
I'd be happy for some hints!
Thanks
|
|
|
|
|
Hi,
1.
whatever pointer operations you want to do to the pixels need to be done in between the LockBits and the UnlockBits.
2.
do you have to have the native code allocate the data buffer? if yes, there is no way around copying all the pixels into the managed image, which is an expensive and unproductive operation. If no, allocating a managed buffer (could be with new Bitmap and LockBits) and passing the pointer to the native world might eliminate the need for a copy operation.
What is the native code you are using?
out of curiosity: what is the make and model of your camera?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Also, if you want to keep a pointer to the pixel bits around,
use the Bitmap constructor that takes an IntPtr. If you pass
a pointer to a pre-allocated array, you can use that pointer
until the array is freed, and you don't need to use LockBits to
access it (although in multithread situations you may need to
synchronize access to it).
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Here's an example of what I'm talking about:
public ref class Form1 : public System::Windows::Forms::Form
{
unsigned char *pBitmapBits;
Bitmap ^testBitmap;
public:
Form1(void)
{
InitializeComponent();
pBitmapBits = new unsigned char[120 * 120 * 3];
for (int x = 0; x < (120*120*3); x += 3)
{
pBitmapBits[x+0] = 0;
pBitmapBits[x+1] = 0;
pBitmapBits[x+2] = 255;
}
testBitmap = gcnew Bitmap(120, 120, 120*3, PixelFormat::Format24bppRgb, IntPtr(pBitmapBits));
}
...
private: System::Void testButton_Click(System::Object^ sender, System::EventArgs^ e)
{
for (int x = 120*3*40; x < (120*3*80); x += 3)
{
pBitmapBits[x+0] = 255;
pBitmapBits[x+1] = 0;
pBitmapBits[x+2] = 255;
}
Graphics ^g = this->CreateGraphics();
g->DrawImage(testBitmap, 10, 10);
}
};
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks a lot you people!
I'm very happy of always getting such good answers.
Respect, for me you are number #1 in the web!
On Topic:
I'll test your way to create bitmap tomorrow, it's late in europe right now
It looks good, but I remember some problems as I wanted to get it done like that.
The cam I use is a monochrome Leutron PicSight P52M-USB, and the only way I get monochrome
Bitmaps done is to assign the Format8bppIndexed - which further made some problems in resizing,
so that I have to work with a temp-Image. I'm not happy with that, but I didn't get a better way yet.
I'd be more happy with not having to work with marshaling between managed and unmanaged. But my company
has "old" native interfaces (one for graphic, one for cam), which must not be changed. So I had to delegate
all function (the way I got it done is also from codeproject) - but it seems to me as a not very stable solution,
to call a managed GUI in creating a thread from native and so on...
Maybe someone wonders about an odd comment, please let me know.
I may be on the wrong way somewhere...
Thanks again for your time, bye!
|
|
|
|
|
Hi,
I have a COM component developed in VC++ which provides hundreds of interfaces.
Many people use this component in their managed code using interop DLLs.
1. I just want to know if this is the right way of doing it?
2. Does it make sense providing a .NET assembly for them to do development in the .NET environment?
3. Is there any advantage if we use .NET assemblies instead of using COM via interop DLLs?
4. Is there any good article that explains how to convert unmanaged COM into managed assembly using managed C++?
Thanks in advance
cheers
Varghese Paul
|
|
|
|
|
I don't know that there is a definitive "right" way to do that. It likely depends on what services your COM components are providing and what the use model is etc. That said you should take a look at how Microsoft provides managed assemblies for using the Office Object Models. They are called Primary Interop Assemblies[^], and how they are different (better) than the standard Interop Generated .NET code.
|
|
|
|
|
Hi!
I have a windows forms control of the following type with 4 Labels in it:
*****************************
*label1......*label2........*
*****************************
*label3......*label4........*
*****************************
Each oft the labels has to fill exactly one quarter of the whole form, because the background color
of the labels is changed at runtime and it doesn't look good if the color fills just the back oft the text.
Labelproperty autosize seems like a simple solution, but if i set it, the label is always just as big as the text is.
Without autosize, it looks not bad, but I don't get it to split in the center.
It looks like that:
*******************************
*label1...............*label2 *
*label3...............*label4 *
*******************************
The rightern labels don't grow with the control, the remain on rightern side in their usual size.
Anyone an idea?
Thanks, cherry
|
|
|
|
|
I would set the Anchor property of labels 1 and 3 to Left, and set labels 2 and 4 to Right (this keeps the left/right edge of the label a fixed distance from the corresponding edge of the form. Then implement on OnResize event for your form, and set the Width of labels 1 and 3, and the Left of labels 2 and 4, like so:
void OnResize(Object^ sender, EventArgs^ e)
{
label1->Width = Width / 2;
label3->Width = Width / 2;
label2->Left = Width / 2;
label4->Left = Width / 2;
}
You may need to add a slight offset to one of these if the labels look like they overlap at all, but this should get you started. You can also do something similar to this for making your labels fill the form vertically.
Dybs
|
|
|
|
|
Thanks Dybs, that might help - I just thought it could be made more nicely.
Something like autosizing with setting the anchors to all sides...
Thanks and bye!
|
|
|
|
|
I've tried setting both the left and right anchors, and it didn't quite get me there. If labels 1 and 2 start out as:
|label 1|
|label 2|
and you set both the Left and Right anchors for both labels and then stretch the form, your labels will overlap like so:
|label 1|
|label 2|
Like I mentioned above, the anchor keeps the edge of your control a fixed distance from the edge of its container. This fixed distance is in pixels, not percentage or proportion, unfortunately. If you find a nicer way to do this though, let me know! This is something I run into all the time, and this is the best I've been able to do so far.
Dybs
|
|
|
|
|
Hi,
this is what I would do:
- add a TableLayoutPanel to your Form; by default it has two rows, two columns, resulting in four identical areas.
- dock or anchor it as appropriate.
- add a label in each cell of the TableLayoutPanel; dock-fill them and set TextAlign=ContentAlignment.MiddleCenter.
Now the labels are centered and their background exactly fills a quarter of the TLP.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Don't know why I didn't think of that. I've spent a good part of this week working on resizing the cells in the TLP!
|
|
|
|
|
Hi All
I Hope this is the right forum to post this query.
I have created a test environment with a server and a client. On the server i have posted a small website with a zip file "abc.zip". When I try to download this file using Webclient with the below code it works fine
hWebClient->DownloadFile("http://170.10.10.80/mySite/abc.zip", "c:\\myFolder\\abc.zip");
When I try to use UriBuilder as shown below i always get the "invalid uri the hostname could not be parsed." exception.
UriBuilder ^hUriBuilder = gcnew UriBuilder();
hUriBuilder->Host = "http://170.10.10.80"
hUriBuilder->Path = "mySite/abc.zip";
hWebClient->DownloadFile(l_hUriBuilder->Uri, "c:\\myFolder\\abc.zip");(Exception)
I have tried to set other parameters as well like
hUriBuilder->Scheme = Uri::UriSchemeHttp;
hUriBuilder->Fragment = gcnew String("main"";
hUriBuilder->UserName = gcnew String("user");
hUriBuilder->Password = gcnew String("passwd");
Tried different combinations of Host like removing "http://" adding another "/" at the end and "http://170.10.10.80/mySite".
but same exception "invalid uri the hostname could not be parsed."
I even tried to use a generic host like "www.codeproject.com" just to check if its the IP address thats causing the problem but even then when i try to access
hUriBuilder->Uri same exception is reported.
Any ideas what wrong i am doing or if i am missing any step.
I am using vs2005, OS XP and server is windows 2003
Thanks
gurindersm
|
|
|
|
|
gurindersm wrote: Any ideas what wrong i am doing or if i am missing any step.
I've never used UriBuilder, however based on the documentation[^] you don't appear to using it correctly. It does not seem that including the protocol "http" in the .Host property is correct.
|
|
|
|
|
Hi,
I have a CLI audio recording program which uses WaveIn and WaveOut to record.
1. I need to prepare buffer and should be able to use for less time i.e., prepare the buffer for 30 mins but should be able to stop at 15 mins and save it to file. I am not able to do this now.
2. I need to get the path of the file. I need to record to a temp file and access it.
The audio's source is Microphone. It can record for custom # of seconds. I mean, when you run VoiceRec.exe and press enter, it would ask you for the # of seconds to record. After I give a number, it prepares the buffer for that time, starts recording and saves it to a file.
But what I want is, I would like to start recording and when the user presses any key, it should stop recording and save it to a WAV file.
http://rapidshare.com/files/240845783/Working_VoiceRecording_2_.rar.html
|
|
|
|
|
Do you really have to ask the user for the # of seconds up front?
Why not just record? Open a FileStream for writing, keep writing data until
the user stops the operation or the harddrive fills up, then close the FileStream.
If you need it buffered in memory as well, use a MemoryStream as well.
Streams....nice and consistent - they are your friends
I bet the FileStream class documentation[^] has all kinds of sample code...
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I don't know much C++/CLI, but my previous experience with streams have resulted in poor performance. Specifically, using a generic stream library ignores issues such as asynchronous I/O for better performance, sizing of I/O to match the underlying I/O system (ie. block size considerations based on filesystem I/O size or disk block size), efficient use of a memory cache with a size matched to the application or expected hardware configurations, and the use of threads around a well sized buffer.
Of course, if this is a "toy" application then this is all superfluous. Or perhaps Microsoft has provided more optimization to their streams than they have in the past?
|
|
|
|
|
I was referring to System.IO.Stream and its derived classes here.
I also assumed that since the source is a microphone, there probably isn't
a large audio bandwidth issue here.
IME, FileStream is more than capable of storing audio as it's captured, at least
to modern drives. MemoryStream works fine as well.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i want to use in my code CTime!!!
any one can help
|
|
|
|
|
how about
1) try some patience & manners
2) use :-
#include <ctime>
[edit] this is asuming you dont mean the MFC CTime class - if you mean that you'll have to add the MFC includes etc if you havnt build an MFC project [/edit]
'g'
|
|
|
|
|
|
#include <atltime.h>
COleDateTime has a better range
#include <ATLComTime.h>
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i'm using window forms application and i'm facing a problem with a variable date_time that hold
a long value 1243930978
I need to convert this number to datetime
Note : when i declare the variable as "CTime sss" it give error undeclared identifier "sss"
any one can help?
|
|
|
|