|
I ran into some problems with the bitmap class and I was given the following advice:
The code below works for me.
Bitmap bmp = new Bitmap(w,h,PixelFormat.Format24bppRgb);
//create drawing surface
float res = 300f; //default resolution of 300dpi
bmp.SetResolution(res,res);
bmp.MakeTransparent(Color.White);
//Create new graphics canvas from the bitmap
Graphics G = Graphics.FromImage(bmp);
//set graphics properties for good bitmap rendering
G.PageUnit = GraphicsUnit.Pixel;
G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
G.CompositingQuality = CompositingQuality.HighQuality;
G.CompositingMode = CompositingMode.SourceCopy;
G.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
G.InterpolationMode = InterpolationMode.High;
//clear the bitmap background (set to white)
G.Clear(Color.White);
// draw a 2px border around the grid image area
G.DrawRectangle(new Pen(Color.Black,2),0,0,w-2,h-2);
....
// draw the rest of the graphics, including text
...
G.Dispose();
// save the image
bmp.Save(imagefilePath,ImageFormat.Bmp);
bmp.Dispose();
This worked fine, up to a point. It enabled me to write a .bmp file to C drive like I intended but all I got was a blank .bmp file without the lines, curves and the labels I wanted displayed.
Can someone knowledgable help me please?
FJ
|
|
|
|
|
I just tried your code and it worked for me, in as much as I got a bitmap with a hollow black rectangle in it. Is that not what you get?
Rob Philpott.
|
|
|
|
|
Hi.
I got this code from this site, I used part of it, what I wanted and needed. I still don´t know how to make my program (that I used the code I mentioned before) write what I want to the .bmp file. All I get is a blank .bmp file with nothing in it. I wanted my program to save a .bmp file to disc (it does that now) but more importantly write some stuff that I tell it to do in this .bmp file (labels with text, lines and curves). Do you know how to do that?
FJ
|
|
|
|
|
Sorry, I'm confused. I took the code you pasted in your original message and turned it into a simple console application. I also added an extra line of code to put a circle in the middle of the bitmap:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
namespace Test
{
class Test
{
[STAThread]
static void Main(string[] args)
{
Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format24bppRgb);
//create drawing surface
float res = 300f; //default resolution of 300dpi
bmp.SetResolution(res,res);
bmp.MakeTransparent(Color.White);
//Create new graphics canvas from the bitmap
Graphics G = Graphics.FromImage(bmp);
//set graphics properties for good bitmap rendering
G.PageUnit = GraphicsUnit.Pixel;
G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
G.CompositingQuality = CompositingQuality.HighQuality;
G.CompositingMode = CompositingMode.SourceCopy;
G.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
G.InterpolationMode = InterpolationMode.High;
//clear the bitmap background (set to white)
G.Clear(Color.White);
G.DrawRectangle(new Pen(Color.Black,2), 20, 20, 60, 60);
G.DrawEllipse(new Pen(Color.Black,2), 20, 20, 60, 60);
G.Dispose();
// save the image
bmp.Save(@"c:\tst.bmp",ImageFormat.Bmp);
bmp.Dispose();
}
}
}
When I compile and run this console application, I get a new file c:\tst.bmp which is a 100x100 bitmap with white background with a 60x60 hollow black square and in the middle of it. Inside the square is a circle. It looks to work fine...
Rob Philpott.
|
|
|
|
|
Since I posted the code for you, I guess I should point out that the comment between the ellipses
:
...
// draw the rest of the graphics, including text
...
means you have some work to do. I have no idea what you want to draw on the graphics surface, but this is where you need to do it. I left in the G.DrawRectangel that draws a border around the bitmap as a sort of example, but the rest is up to you.
The secret to this is that by creating the graphics surface from the bitmap (you decide on the size and set the w (width) and h (height) parameters approprietly,
then draw the rest of the content (Use any of the Drawxx Methods in the System.Graphics namespace). Then when you save the bitmap, it will contain what you have drawn. Without seeing the code you were using to 'create' your lables, this is the best I can offer. If you will post the code you are using now (or at least some examples, I will try to flesh out the example further.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
-- modified at 18:53 Tuesday 25th October, 2005
|
|
|
|
|
I'm having a problem with an exception thrown by a backend (non gui) class being intercepted by the framework before it has a chance to bubble up to the GUI layer to display the appropriate error message.
Trying to localize the probem I wrapped every call going up the stack in this sort of try/catch block:
try
{
/*calling statements*/
}
catch
{
throw;
}
Tracing it via the debugger, it's successfully thrown out of the assembly where it occurs, is caught and rethrown by the first two methods inside the gui, from the second method (which is a gui control event handler if it's relevant) instead of propagating farther up the callstack, it's intercepted by the framework which displays a details/continue error and eats the exception.
I could move the error generation and shutdown code to a place in the callstack before this happens, but the same error can (potentially) occur elsewhere in the app, and in all cases should result in an identical action (shutdown), so I'd like to handle it in only one place, with the other fatal exceptions which are handled inside Main();
|
|
|
|
|
|
I don't think so. appDomains only come in during IPC, right? MY apps single threaded, and the exception gets stolen going between two methods of a single class.
|
|
|
|
|
dan neely wrote: which is a gui control event handler if it's relevant
Event handlers for GUI controls run on the UI thread, so any exceptions thrown from them will cause the message loop to terminate and Application.Run to return. How are you attempting to propagate the exception from the event handler?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
I didn't know that, and didn't do anything special. Is there a way to make it pass through? If not, I can change the structure of the app enough to avoid the problem.
|
|
|
|
|
I have the following enum:
[TypeConverter(typeof(E1C))]
public enum E1
{
a = 1,
b = 2,
c = 3
}
And the following converter:
public class E1C : EnumConverter
{
public E1C(): base(typeof(E1))
{
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if(value != null)
{
if(destinationType == typeof(string))
{
if((E1)value == E1.a)
{
return "a a a";
}
else if((E1)value == E1.b)
{
return "b b b";
}
else if((E1)value == E1.c)
{
return "c c c";
}
}
if(destinationType == typeof(InstanceDescriptor))
{
FieldInfo fi = EnumType.GetField(value.ToString());
if(fi != null)
{
return new InstanceDescriptor(fi, null);
}
}
}
return base.ConvertTo (context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value != null && value is string)
{
if((string) value == "a a a")
{
return E1.a;
}
else if((string) value == "b b b")
{
return E1.b;
}
else if((string) value == "c c c")
{
return E1.c;
}
}
return base.ConvertFrom (context, culture, value);
}
}
Now, on the property page every thing works fine, as well as in ASP .net page,
but within Windows Form, the designer generates the folowing code:
#region Windows Form Designer generated code
...
...
this.userControl11.E1 = WindowsApplication2.E1.b b b;
...
...
#endregion
As you can see, instead of generating:
this.userControl11.E1 = WindowsApplication2.E1.b;
It generated:
this.userControl11.E1 = WindowsApplication2.E1.b b b;
If anyone know this problem and its solution, it would be nice!
Thanks in advance!
Ilan
|
|
|
|
|
I posted this message in ASP.net by mistake.
However, minhpc_bk has answer this question, and it works fine!
Here is the link to the answer of the same question (if anybody is interested).
The answer
|
|
|
|
|
Hi All,
I got a big problem i have written some dlls in c# and they are working great in c# but i cant use them with c++(i can use the ones written in c++)
i am using
GetModuleFileName
and
GetProcAddress
commands GetProcAdress cant get the adress.
I know dlls are IL code so its compiler independent.
Thanks
|
|
|
|
|
Well if you mean "C++" as in "unmanaged C/C++" then you need to use some other mechanism like ActiveX because the two runtimes aren't the same (it is the same as expecting VB6 to use any C++ shared object as is). COM and ActiveX provide a language independent mechanism and hosting to bridge different runtime environments that would normally not be able to interact.
If you maen "C++" as in "managed C++ that runs on .Net" then it is all IL and you should be able to address the objects naitively in either language binding. Managed C++ obeys all of the same assembly loading rules as C# assemblies.
|
|
|
|
|
I am using .NET
I am using the code below i simplifed everything but it is not working
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#define MAXMODULE 50
using namespace std;
typedef int (WINAPI*cfunc)();
cfunc tests;
int main() {
HINSTANCE hLib=LoadLibrary("basic.dll");
if(hLib==NULL) {
cout << "cant load dll" << endl;
getch();
return 0;
}
char mod[MAXMODULE];
GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
cout << "dell loaded" << mod << endl;
tests=(cfunc)GetProcAddress((HMODULE)hLib, "test");
if(tests==NULL) {
cout << "cant load functions" << endl;
FreeLibrary((HMODULE)hLib);
getch();
return 0;
}
int x = tests();
cout<
|
|
|
|
|
That won't work for managed dlls. Either use them as COM objects, or use Managed C++ to interface to them.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
I need some help, bad. In trying to make a external macroing program. Were im stuck is at the part were you have the program automaticly send a key message to your program of choice. An example would be I hit the 'a' key and that tells the program to run a script. That uses keys and delay timers. All the code I have found is just about getting what key is pressed. I need the program to tell a nother program. Hay use this key then this key ect.
Thanks
|
|
|
|
|
it's possible to use hooks to send messages from one app to annother (search the forum for examples). IF you want to do something beyond what can be easily done by feeding the controlled app simulated user input you're dependent on the apps authors having provided some means to facilitate automation.
|
|
|
|
|
Thanks Dan, Ill see what i can dig up
|
|
|
|
|
I've posted this question before but i've not specified an important thing.
If i open a File in FileStream with FileAccess.Write, it is locked so i can't rightclick on it and copy or delete or move...
Is it possible in any way? ( I don't close the application or close the stream)
|
|
|
|
|
The operating system is being "nice" by honoring the file lock. What possible use case do you have to move a file that is still opened and being written too?
|
|
|
|
|
simply becouse it is a file that is created by my program and it is used just by my program.
When my program close the file is deleted.
I want to know if someone in some way can get that file durign my application running if my application is using it!!!
|
|
|
|
|
It is in the end just bits on a disk. If someone wanted to they could read it (unplug/cause a kernel memory dump, reboot and read the file). The file locking mechanim is strictly not a security feature: it is there to maintain a consistent IO for running applications. If you really want a file that is secured, then you must use security mechanism.
|
|
|
|
|
Hi!
I have already posted this question in this forum, but I have not yet got any response. So i have decided to post it again.
I am trying to create a RubberBand when cropping an image from a PictureBox using the mouse.
All is well when drawing the rectangle with the help of MouseDown, MouseMove and MouseUp events.
I don't know how to maintain aspect ratio of the rectangle when drawing it over the picture box (as in Adobe PhotoShop).
I can mail you my code for your reference.
Any Idea will be helpful.
-- modified at 12:12 Tuesday 25th October, 2005
|
|
|
|
|
I would think a simple approach to doing this would be to define a double aspect which you set to height/width of the original image (you'd need to put some checks in to avoid possible divide by zero exceptions here).
Then when tracking the rectange take whichever is largest of the (absolute) new width or new height and determine the value of the other variable (new height or new width) by multiplying/dividing this by aspect ...if that makes any sense?
If you think of a straight line being defined as y = mx + c, the gradient (m) of a line going through the bottom left and top right corners of the rectange should remain constant whatever the size of the rectangle.
Rob Philpott.
|
|
|
|