|
We're doing some bitmap manipulation and have come up against the issue of not being able to create a 2bpp bitmap. So we've implemented the following class:
<br />
public class BitmapSubsystem<br />
{<br />
public static readonly BitmapSubsystem Instance = new BitmapSubsystem();<br />
<br />
protected ColorPalette GetMonoColorPalette()<br />
{<br />
Bitmap bitmap = new Bitmap( 1, 1, PixelFormat.Format1bppIndexed );<br />
<br />
ColorPalette palette = bitmap.Palette;
<br />
palette.Entries[0] = Color.FromArgb( (int)0xFF, (int)0, (int)0,(int)0 );
palette.Entries[1] = Color.FromArgb( (int)0, (int)255, (int)255,(int)255 );
<br />
bitmap.Dispose();
<br />
return palette;
}<br />
<br />
public void ConvertTo2bppBitmap( Bitmap sourceBitmap, Stream output )<br />
{<br />
int Width = sourceBitmap.Width;<br />
int Height = sourceBitmap.Height;<br />
<br />
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);<br />
<br />
try<br />
{<br />
ColorPalette pal = GetMonoColorPalette();<br />
<br />
bitmap.Palette = pal;<br />
<br />
BitmapData bitmapData;<br />
Rectangle rect = new Rectangle(0, 0, Width, Height);<br />
<br />
bitmapData = bitmap.LockBits( rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);<br />
<br />
IntPtr pixels = bitmapData.Scan0;<br />
<br />
unsafe<br />
{<br />
byte * pBits;<br />
if (bitmapData.Stride > 0)<br />
pBits = (byte *)pixels.ToPointer();<br />
else<br />
pBits = (byte *)pixels.ToPointer() + bitmapData.Stride*(Height-1);<br />
<br />
uint stride = (uint)Math.Abs(bitmapData.Stride);<br />
<br />
for ( uint row = 0; row < Height; ++row )<br />
{<br />
for ( uint col = 0; col < Width; ++col )<br />
{<br />
<br />
byte * p2bppPixel = pBits + row*stride + col;<br />
<br />
Color srcPixel = sourceBitmap.GetPixel((int)col, (int)row);<br />
<br />
double luminance = (srcPixel.R *0.299) + (srcPixel.G *0.587) + (srcPixel.B *0.114);<br />
<br />
if( luminance > 0 )<br />
*p2bppPixel = (byte)(1);<br />
else<br />
*p2bppPixel = (byte)(0);<br />
<br />
} <br />
} <br />
} <br />
<br />
bitmap.UnlockBits(bitmapData);<br />
<br />
bitmap.Save(output, ImageFormat.Gif);<br />
}<br />
catch<br />
{<br />
}<br />
<br />
bitmap.Dispose();<br />
} <br />
This uses some unsafe pointer manipulation to copy the bitmap across to a 2bpp image with transparency, and does the job nicely.
Question is, can this code be rewritten as managed code (using SetPixel/GetPixel)? I know it'll be a little slower, but we're only working on bitmaps that are 40px by 16px, so performance isn't too much of an issue. But SetPixel doesn't seem to allow me to set pixels on a 2bpp image - it seems to expect an ARGB value, rather than a pallette index....
If it can't be written as managed code, what are the implications of publishing unsafe code like the above? Is there a risk to my production webserver?
________________________
http://www.webreaper.net
|
|
|
|
|
Anyone?
________________________
http://www.webreaper.net
|
|
|
|
|
Hallo,
I'm a real beginner with C# and I have a silly problem with one DataTable combined with a DataGrid.
Short describing of surround:
I have one DataSet with three DataTables, each shown in DataGrids.
The DataSet is persistent (typed DataSet) as .xsd-file.
DataBase ist MSDE. All three DataTables are filled with the
DataAdapter.(DataSet, DataTable).Fill-Method.
If I execute the fill-Methode for one table twice or more, in my datagrid the records are added. To avoid this behaviour, I want to use the
DataSet.Tables["table"].Clear()-Method
before executing the Fill-Method. With two DataTables it works, but with the third, the DataGrid stays empty after the Fill-Method.
- in the debugger I can see that there are records in the DataTable
- when I click into the DataGrid, an error occurs that I try to translate (German version):
"DataBinding could not find line in list that can be used vor all bindings."
Help would be very appreciated.
Thanks
Vexta
|
|
|
|
|
Hi All
I have a form that holds multiple checkboxes. Currently the application, upon pressing a button, iterates through the controls on the form, determining which ones have been checked. It then takes appropriate action according to the selection. I use a foreach statement to iterate through the controls. Is there any way I can fix the order of iteration? The problem is I have another form that has the same checkboxes as the first but also adds a few. The method used to determine the action taken counts the controls as they are iterated through to learn the location of the checked control. However, in the second form the route through the controls using foreach is different making the count different. So any comparison of the count is invalid. I hope this makes sense.... The location of the checkbox in the count determines the action taken by the program. Multiple selections are possible.
Should I use the tag property to ensure or is there a way to for the foreach statement to follow the same path in both forms?
Kind Regards
Jamie
|
|
|
|
|
There is no way to alter the order in which the controls are iterated through. They come back in the order in which they were instantiated.
It sounds like your code needs to be modified so that the checkboxes update a central variable in the checkboxes own Click event handlers. The handler can update a count variable or modify a bit field as needed. Then your code doesn't have to iterate through or count anything, it was already done on-the-fly.
I'm sorry if this doesn't fit your requirements. I'm not sure why your couting anything and have even less of a clue why the order of the controls matters.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi,
I was wondering how to convert a string like:
string myString = "1:00 PM,2:00 PM,8:00 PM,9:00 PM";
into a string array (or even better a DateTime array).
thanks,
Ron
|
|
|
|
|
string[] splitted = myString.Split(',');
To convert the result to a DateTime array you will have to loop through it and make it manually (Convert.ToDateTime on each element).
|
|
|
|
|
|
Hope this helps...
string myString = "1:00 PM,2:00 PM,8:00 PM,9:00 PM";
string[] myStringArray;
DateTime[] myDateArray;
// Split myString into an array
myStringArray = myString.Split(new char[] { ',' });
// Create an array to hold our times
myDateArray = new DateTime[myStringArray.Length];
// Step through our string array and create the times
for (int index = 0; index < myStringArray.Length; index ++)
myDateArray[index] = DateTime.Parse(myStringArray[index]);
|
|
|
|
|
I'm at the level in my programming experience where I understand algorithms and functions well, but I'm having a hard time conceptualizing classes and how they interact. Here's my problem:
I've got three classes: System, Room, and User. I need to dynamically instantiate multiple Rooms and multiple Users from the System class. Of course, accessing a particular Room or User from the System is easy enough, if I know the name, but I also need to access members of the System class from my Room classes, as well as members of the User classes from my Room classes.
So, my dilemma is twofold. First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated?
Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User?
Or am I trying to bend unbreakable rules and need to go back to the drawing board? I appreciate any insights.
|
|
|
|
|
tantiboh wrote:
First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated?
You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor.
tantiboh wrote:
Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User?
In the absence of multiple inheritance, you can use interfaces here. Define an interface that describes the information you need to get in common from all three class types ( such as name ), and one for any stuff you need from each class seperately. You may want to also write Fascade classes, for example a RoomFascade class would be used to provide methods that help you work with a Room, but would bloat the Room class if they were all put in there.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Christian Graus wrote:
You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor.
Interesting. Tell me if I'm off base here: let's say I instantiate Player and give its Name property as "xyz". I can make that Player part of a hashtable off its Name property, and anytime I need to get to it, I can get the hash for its Name property and I've got it.
So, if that's all well and good, then I've got a dozen players, all neatly placed into a hashtable that is part of the System class. That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System.
Christian Graus wrote:
In the absence of multiple inheritance, you can use interfaces here.
I'll research interfaces more, but the little I've absorbed so far tells me that it should allow me to access my System class from any Room class. If that's the case, then I should be able to access the Player hashtable in the System class from the Room class, giving me the Player class' "name." However, at that point, I'm stuck. How do I actually use that name to talk to that Player class from my Room class, since it wasn't instantiated there?
Thanks for your help.
|
|
|
|
|
That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System.
Make the room class keep a reference to the system object that created it, or just keep a reference to ur player hashtable. U can pass this reference through the room's constructor.
public Room(System owner){...}
Everytime u need to get some player from a room object u can get it through your System object that "owns" the room and hass all the necessary information.
|
|
|
|
|
Excellent. I'll start toying with it. Thanks.
|
|
|
|
|
Can anyone tell me difference between GetType() method, is operator and TypeOf() operator I'll be very much thankful to you.
Regards.
Ruchir.
RuchirDhar Dwivedi
Software Engineer
Windowmaker Software Pvt.Ltd.
Baroda, India.
|
|
|
|
|
The typeof operator returns an instance of System.Type corresponding to the class name you passed it to. You can't determine the type of an object using the typeof operator. For example
class Foo{}
class Test
{
public static void Main(string []args)
{
Console.WriteLine(typeof(Foo));
Foo f = new Foo();
Console.WriteLine(typeof(Foo));
}
}
The GetType function gets you the runtime type of an object. Note that it is the *runtime* type and not the static type. For eg.
public void SomeFunc()
{
object obj = new Foo();
Console.WriteLine(obj.GetType());
}
will print Foo and not Object.
The is operator checks the runtime type of the arguments and sees if the LHS is (or can be cast to) the RHS. It returns true if the casting can succeed and false otherwise. For eg
public void SomeFunc()
{
Foo f = new Foo();
Console.WriteLine(f is Foo);
object obj = new Foo();
Console.WriteLine(obj is Foo);
object obj2 = new object();
Console.WriteLine(obj2 is Foo);
}
Hope this helps.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
devepoed code. worked fine. ported the code to my laptop. when I try and run it I got an error.
The Microsoft Jet database engine cannot open the file 'D:\MGDevelopment\view\DB\simdac_be.mdb'. It is already opened exclusively by another user, or you need permission to view its data.
I could not get the error to go awayso I created a new project on my laptop and simulated the code. I get the same problem. What could prevent the code from accessing he data?
I am using oledb coonection and oledbdatareader.
Brian
|
|
|
|
|
check the first application which has opened the access file, it should open that file as a shared mode
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
I have used crystal report on my program also build setup project that include merge module Crystal_Managed2003.msm and Crystal_regwiz2003.msm.
also placed license key in Crystal_regwiz2003.msm,but i always get the error "CrystalDecisions.CrystalReports.Engine.LoadSaveReportException: Load crpe32.dll failed."when i try to open crystal report.Can any one help
shailee
|
|
|
|
|
After having written the same thing, the thing that I found with this problem and might not be the case with you though. Is that the crystal library your are using doesn't match the merge modules that you are trying to merge into your program.
Another thing that you might try, is to see if the key that your inserting is the correct key for the merge module as well.
For more information I would head over to Crystal Decisions website and have a look at the support site there. It helped me.
|
|
|
|
|
well the version of the library and modules are same and the key is correct too.
but when i run the setup in another computer and install the program any try to open crystal report I get this error
do I need to register crpe32.dll ?
I am waiting for the reply of Crystal report's support site.....
but if anyone has the solution please......
help help help
|
|
|
|
|
I would like to invoke any control's event just as if the user or another source would. This, of course without building my own derivated class.
I've been looking around but i just dont seem to find the answer.
I tried the following but didn't work.
//What i'm trying to do here is to invoke a form or control event by code.
//The idea seems right but i just could get it to work.
Form1 _fasoc = new Form1();
Type f = _fasoc.GetType();
System.Reflection.EventInfo ev = f.GetEvent("DoubleClick");
System.Reflection.MethodInfo minfo = ev.GetRaiseMethod();
//minfo gets null but the event is recognized. What's wrong?
minfo.Invoke(_fasoc, new object[]{this, new EventArgs()});
If anyone knows about this, please help me out.
|
|
|
|
|
In my application I want to change the decimal symbol "," into "." without changing from control pannel regional settings..I searched the net but connot find any good answers..They are talking about something like "CurrentCulture.CultureInfo.NumberFormat" Can anyone help me
Thanx a lot..
|
|
|
|
|
Yep - because it's a , in Germany, and other countries, you need to change the localisation setting to use whatever country you are trying to emulate.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Can anyone tell me how you can add a image to a context menuItem??
if i use the drawItem event i doesnt work:
Font drawFont = new Font("Arial", 10);
e.Graphics.DrawRectangle(new Pen(Brushes.Black, 1), 200,200, 100, 20);
e.Graphics.DrawImage(Image.FromFile(@"D:\Ontwikkelmap\Visual studio projects\Marquis poolcompetitie\Pool Competitie\bin\Debug\Images\print.gif"),200,200);
e.Graphics.DrawString("test", drawFont, Brushes.Red, 0, 30 );
when i use the same code with a mainMenu, it works...
|
|
|
|
|