|
First off, thank you very much for the help so far. I am a little unclear on what exactly you mean here. Could you please be more specific what property I need to implement to make this work with a foreach statement? I've been looking and can't seem to find any documentation on it. Everything I've seen seems to assume that you only want to iterate for one data type. Is what I'm trying to do even possible?
|
|
|
|
|
I thought you could create properties which return an IEnumerable<T>, and then those would allow a single class to specify several types which could be iterated over.
So you end up with
foreach(int n in myClass.Ints)
and
foreach(string s in myClass.Strings)
you can't have more than one type for
foreach(string s in myClass)
Christian Graus - Microsoft MVP - C++
"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 see what you're saying now. Thank you very much for your help.
|
|
|
|
|
Hi,
with the given error message I assume you have a class like this:
class SampleClass : IEnumerable<int>, IEnumerable<string> { ... }
If you want to iterate over the ints then do:
SampleClass mySampleInstance;
foreach (int i in (IEnumerable<int> )mySampleInstance) { ... }
or if you want to iterate over the strings:
foreach (string s in (IEnumerable<string> )mySampleInstance) { ... }
Robert
|
|
|
|
|
Hi, I want to save data in a .dat file, but when I open the created .dat file, I can read the text in it. How can I make the data unreadable? Thanks everyone. -Bruce
|
|
|
|
|
Hi,
you could create a binary file (use BinaryWriter instead of TextWriter), it would be
faster and more compact, but then you could still recognize (most of) the string data.
and you could use cryptography; .NET has some classes for this.
It all depends on how secure you want your data, and how much effort you want to put in it.
|
|
|
|
|
We are making a project using C#,it is an image processing project, we want to calculate the generalized eigenvectors and eigenvalues for two matrices...
In the DotNetMatrix I found the calculation of eigenvectors and eigenvalues but for one matrix only
Can anybody help?
Thanks,
m w
|
|
|
|
|
Hi,
please read the Christian Graus's articels related to the image processing from this forum ........it is a very usefull and helpful for biggner .........i like these articles.....you can also search the other author's articles.
|
|
|
|
|
Hi,
By your help I able to draw the any shape into the pictureBox and able to save the drawing.
It is a simple way to save any image from the pictureBox
pictureBox2.Image.Save(saveFileDialog1.FileName);
but above command do work when any image is already load onto the pictureBox.otherwise some error occur like that:
"NullReferenceExpection was unhandled !"<br />
"Object reference not set to an instance of an object."
“this is because you can't actually draw inside a picturebox. Although it may look like you're doing so, you are in fact drawing 'on top of' the picturebox and the image is not changed. So, if you haven't loaded an image, then the Image property will still be null.”
“If you use a picturebox, put a picture in it, and draw on it first. Then you can save it. CreateGraphics should be used only for drawing things you want to be able to erase, such as rubber bands. It is not for persistent drawing, and certainly it makes your picture box a waste of time, it never does anything”
Solution of above problem is that
“You need to make the graphics drawn on the PictureBox to be a persistent bitmap then only you can save the image.
Use a bitmap to draw the images upon and then reflect it upon your pictureboc. When you want to save just save the bitmap.”
So, I use follwing command to draw any thing
Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);<br />
Graphics g1 = Graphics.FromImage(bmp);<br />
Graphics g2 = pictureBox.CreateGraphics();<br />
Pen p = new Pen(Color.Red, 5);<br />
g1.DrawEllipse(p, e.X, e.Y, 50, 70);<br />
g2.DrawEllipse(p, e.X, e.Y, 50, 70);
And use follwing command for save the drawing
if (saveFileDialog1.ShowDialog() == DialogResult.OK)<br />
{<br />
<br />
bmp.Save(saveFileDialog1.FileName);<br />
<br />
}
Am I success???
You notice that I use both bitmap and pictureBox at a time
Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);<br />
Graphics g1 = Graphics.FromImage(bmp);<br />
Graphics g2 = pictureBox.CreateGraphics();<br />
g1.DrawEllipse(p, e.X, e.Y, 50, 70);<br />
g2.DrawEllipse(p, e.X, e.Y, 50, 70);
I used bitmap for save(hard copy) the drawing and pictureBox used for seeing(soft copy) the image from the screen ………Am I right?
Many Thanks
|
|
|
|
|
Why are you both drawing on the image and on the screen? You are still just drawing on the screen where the picture box happens to be, so the graphics will disappear whenever the window updates.
Why not just how the image in the picture box?
---
single minded; short sighted; long gone;
|
|
|
|
|
if i use only Bitmap like as
Graphics g1 = Graphics.FromImage(bmp);
then drawing do not appear on the screen. i used Bitmap for save the drawing...............So i also used
Graphics g2 = pictureBox.CreateGraphics();
if you have other solution then tell me........
thanks
|
|
|
|
|
The reason it's called a picturebox is that it has a property called Image, which you can pass an Image or Bitmap to, and it will draw it to the screen. You really should buy a book on WinForms and read it.
You need to call Invalidate() on the control to get it to redraw itself when you change the Bitmap, but if you set the Bitmap you're drawing to, to be the Image of the control, it will draw it for you.
Christian Graus - Microsoft MVP - C++
"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 )
|
|
|
|
|
zeeShan anSari wrote: if you have other solution then tell me...
I just did.
---
single minded; short sighted; long gone;
|
|
|
|
|
zeeShan anSari wrote: Graphics g2 = pictureBox.CreateGraphics();
Looks like I answered you before. This is a total waste of time. g1 draws to the Bitmap, and the picturebox displays the bitmap. CreateGraphics is both superfluous in this case, and still as useless as the last time you asked about it.
zeeShan anSari wrote: I used bitmap for save(hard copy) the drawing and pictureBox used for seeing(soft copy) the image from the screen ………Am I right?
You're wasting your time drawing to the picture box, what did you think a picture box is ? It's a control that displays a bitmap 'for seeing'.
Christian Graus - Microsoft MVP - C++
"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 )
|
|
|
|
|
hi
i want to know how to read,delete,insert,update database(Ms Access)using c#.if anybody know please reply.
|
|
|
|
|
Hello,
The best way to get the desired result fast is to use ADO.NET connections and commands.
Tutorials can be found here: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
|
|
|
|
|
Look through these articles C# Databases[^] here on CP. If you get stumped, ask a more general question. Your post is way to broad...
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
If his post was already too broad, asking an even more general question wouldn't help. He should ask a more specific question.
The best way to accelerate a Macintosh is at 9.8m/sec² - Marcus Dolengo
|
|
|
|
|
Maybe I should have rephrased general question as specific question
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi.
I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException"
The decoder comes an an executable which I've turned into a .dll. This decoder has about a ton of structures, most of the containing other structures, arrays of structures, and unions of structures.
I need help converting the following to managed code:
The unmanaged structure is this:
typedef struct<br />
{<br />
vc1_eBlkType eBlkType; <br />
FLAG Coded; <br />
union<br />
{<br />
vc1_sBlkIntra sIntra; <br />
vc1_sBlkInter sInter; <br />
} u; <br />
} vc1_sBlk;
As you can see it contains an enumerator and a union of another type of struct.
I've taken the unmanaged vc1_sBlkInter structure
typedef struct<br />
{<br />
vc1_NumZeroCoef NZC; <br />
HWD16 DC; <br />
HWD16 ACTop[7]; <br />
HWD16 ACLeft[7]; <br />
HWD16 SmoothRows[16]; <br />
} vc1_sBlkIntra;
And turned it into a managed structure :
[StructLayout(LayoutKind.Sequential)]<br />
public unsafe struct vc1_sBlkIntra<br />
{<br />
ushort NZC; <br />
short DC; <br />
<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACTop; <br />
<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)] public short[] ACLeft; <br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public short[] moothRows; <br />
};
Now, I am trying to turn vc1_sBlk into a managed structure as well.
The last attempt is this
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]<br />
public unsafe struct vc1_sBlk<br />
{<br />
[FieldOffset(0), MarshalAs(UnmanagedType.I4)]<br />
public vc1_eBlkType eBlkType; <br />
[FieldOffset(4), MarshalAs(UnmanagedType.AsAny)]<br />
public byte Coded; <br />
[FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sIntra; <br />
[FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst = 64)] public vc1_sBlkIntra sInter; <br />
} ;
But I have pretty much tried every kind of marshalling mentioned online, but I keep getting this error:
"Could not load type 'vc1_sBlk'...because it contains an object field at offset 5 that is incorrectly aligned or overlapped by a non-object field."
I am really stuck here and I can't find an answer to this anywhere.
ANY help would be greatly appreciated.
Tom
|
|
|
|
|
AFAIK value types in CLR must be "naturally aligned", meaning their address (and hence
also their offset within a struct or class) must be a multiple of their size.
So you cant put an int at an offset of 5.
But on the other hand structs in C/C++ get padding bytes by default to achieve also
"natural alignment".
(I think there is a switch to turn this off, but one normally does not do that).
You should be able to check this either by printing some actual addresses (in hex!),
or by showing some offsetof() results, in your C/C++ code.
If so, this means sIntra in vc1_sBlk is actually at offset 8.
Lucky for you, now you can have C# and C/C++ agree and work together.
|
|
|
|
|
Hi Luc.
Thanks for the replay, but it brings up some new questions.
First about the offset. The way I figured it was that offset is the number of bytes to offset. So for one int and one short the offset should be 5 bytes.. Am I getting this wrong.
I also tried changing the offset to 8, but now I am getting this error
"Cannot marshal field 'sPosition' of type 'vc1DEC_sState': The type definition of this field has layout information but has an invalid managed/unmanaged type combination or is unmarshalable."
when I am trying to marshal the IntPtr that points to the structure type 'vc1DEC_sState' in the VC1 decoder. This structure hold the structure 'sPosition' which holds another structure, which in turn holds the 'vc1_sBlk' structure in which I changed the offset of 'sIntra'.
So my C# and C have yet to agree, but I will try to somehow get the size of 'vc1_sBlk' and of 'sIntra' from the C code.
If you (or of course anyone else) have more ideas, I really need them.
10x
|
|
|
|
|
TomWolfstein wrote: that offset is the number of bytes to offset
correct
TomWolfstein wrote: So for one int and one short the offset should be 5 bytes
wrong. short is 2B.
Anyway, keep comparing/checking the offsets on both sides until they agree.
And of course, if you dont want to transfer some sub-struct from one side to the other,
just make it an IntPtr.
One more warning: long on CLR means 8B, not 4B (and IntPtr is either 4B or 8B depending
on Win32/Win64).
One more thought: maybe it is wise to fill the alignment gaps with explicit variables,
something like byte myReserved1; byte myReserved2;...
Sorry that's it for now; I am not available tonight...
|
|
|
|
|
Hi.
You are right about the short of course. I meant byte and got confused with the marsahlled arrays.
The problem with IntPtr (and with padding) is that if the managed struct is not defined exactly as the unmanaged struct, it gets garbled when marshaling the IntPtr to struct and I can't get the data I need.
Thankss for your help so far.
If you think of anything else later, I'll be happy to hear (read) it.

|
|
|
|
|
Hi Tom,
1.
please read again my earlier post about the padding bytes, and check it for the C side.
I am rather sure it is true C/C++ inserts them. You should verify to make absolutely sure.
My claim is: a C struct {char; int;} will have a size of 8, and probably
a C struct {int; char;} will too. Please check both.
2.
where you write
[FieldOffset(5), MarshalAs(UnmanagedType.Struct, SizeConst=64)]
I would replace 5 by 8 as explained before,
and I would drop the SizeConst=64 since that applies only to arrays and strings, not structs
(so I guess you could drop the MarshalAs(UnmanagedType.Struct too since that does not
add any information)
3.
my remark about IntPtr was not to the point; it applies to classes (in case you dont need
them), it does not apply to structs. For structs whose value you dont need you can omit them,
as long as you keep the FieldOffset values correct.
(Temporarily) omitting some substructs may help you in verifying that the Marshaling of
the remainder succeeds (before you got all the details right in those omitted substructs).
4.
You can always instantiate a struct and get its Marshal.Sizeof() which means the number
of bytes that will cross the border given the current information (as in FieldOffset
and MarshalAs attributes). This sizeof should match the C-side sizeof() value.
So I suggest you add logging to both sides to actually see them.
|
|
|
|
|