|
As they've said your program still has a lock on the files. I would guess that when the image is loaded into the BitmapImage, it locks the file. See this blog: “Memory leak” with BitmapImage and MemoryStream[^]
It doesn't look like it has a dispose method, so you're best bet is to do what others have suggested and use a stream to read it in yourself.
|
|
|
|
|
read the images with streams - no lock is set on the file.
btw. I'm not sure you want to use 'System.Environment.CurrentDirectory' as it is relative to the point of application launch (shortcuts) and may change easily.. if you meant the application directory use Application.StartupPath
life is study!!!
|
|
|
|
|
Hi,
Im using windows form combobox, binding the value using Binding object,after retreving from the database,im using Combobox.selectedvalue="2"; after the form load completion the selected value is not get retaining. im using combobox style as dropdown.
Regards
Kanna
|
|
|
|
|
Try to set this Combobox.selectedvalue="2"; value (through code) in the loaded event of the form.
My signature "sucks" today
|
|
|
|
|
I assume you try setting the value after you bind the control to your data..
if the value is bound to int datacolumn then drop the " - use integers..
also you may try setting ComboBox.BindingContext property to parenting Form.BindingContext.
ex. cmb.BindingContext = this.BindingContext;
life is study!!!
|
|
|
|
|
Hi,
I have the picture box in which i draw the rectangle (code is below) and inside that rectangle i typed some name using label box. Now i try to store these. How can i do this using c#
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
string file = "";
OpenFD.FileName = "";
OpenFD.Title = "open image";
OpenFD.InitialDirectory = "C";
OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
if (OpenFD.ShowDialog() == DialogResult.OK)
{
file = OpenFD.FileName;
pictureBox1.Image = Image.FromFile(file);
sz = pictureBox1.Size;
a=sz.Width; b= sz.Height;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
{
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
this.Invalidate();
Application.DoEvents();
break;
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Pen pen = new Pen(Color.Red, 2)) e.Graphics.DrawRectangle(pen, rect);
if (label1.TextAlign == ContentAlignment.TopLeft)
{
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
}
else if (label1.TextAlign == ContentAlignment.TopCenter)
{
SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
float left = ((float)this.Width + label1.Left) / 2 - size.Width / 2;
RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
}
else
{
SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
float left = (float)label1.Width - size.Width + label1.Left;
RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
}
label1.Top = rect.Top; label1.Left = rect.Left; label1.Width = rect.Width; label1.Height = rect.Height;
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFD1 = new SaveFileDialog();
SaveFD1.FileName = "";
SaveFD1.InitialDirectory = "C";
SaveFD1.Title = "save file Name";
SaveFD1.Filter= "JPG|*.jpg|Bmp|*.bmp";
if (SaveFD1.ShowDialog() != DialogResult.Cancel)
{
System.IO.Stream filename = (System.IO.FileStream)SaveFD1.OpenFile();
if (SaveFD1.Filter == "JPG")
pictureBox1.Image.Save(SaveFD1.FileName);
System.Drawing.Imaging.ImageFormat.Jpeg);
else if (SaveFD1.Filter == "Bmp")
{
pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
}
filename.Close();
}
}
On using the above code i just load and saved the image (without drawing any rectangle). Now tried to open the saved image but its shows the error as "Out of memory".
Please Can any one tell where i am going wrong....
|
|
|
|
|
You're going to have some problems with the method you're using if you want to save it.
The best thing that I can suggest to do is to create an intermediate Bitmap to draw on and set your PictureBox1.Image value to that Bitmap. Then, you can save it pretty easily. For example:
Rectangle rect = New Rectangle(0,0,0,0);
Bitmap myBitmap;
private void PictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (myBitmap == null)
{
myBitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height, PictureBox1.CreateGraphics());
PictureBox1.Image = myBitmap;
}
rect = New Rectangle(e.X, e.Y, 0, 0);
PictureBox1.Invalidate();
}
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = New Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
PictureBox1.Invalidate();
}
}
private void PictureBox_Paint(object sender, PaintEventArgs e)
{
if (myBitmap == null) { return; }
using (Pen pen = New Pen(Color.Red, 2))
{
using (Graphics g = Graphics.FromImage(myBitmap))
{
g.Clear(Color.White);
g.DrawRectangle(pen, rect);
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (myBitmap != null)
{
myBitmap.Save("D:\\Temp\\Bitmapsave.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
It's basically the same thing you've done, except instead of drawing on the bitmap instead of directly on the PaintBox.
|
|
|
|
|
Hi,
Thanks for replay. I tried the code and still I get "out of memory" error when i try to open the saved image.
error at
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
....
.....
pictureBox1.Image = Image.FromFile(file); (this line shows as out of Memory)
..........
}
in the code, pre-defined the location to save. but I am giving it in run time. is this causing error.
is the image needs to saved in temp. i saved the image in mydocument\mypicture\. I went to the saved location and the saved image shows as no preview available.
is there is any change needs to be done in Open toolstripmenu.
Thanks
|
|
|
|
|
You're not understanding what you are actually doing and how that isn't what you are trying to do.
First, let me try to explain what your Paint method is doing. When you use the Graphics provided by the Paint method and then draw rectangles and other objects over that, none of that gets saved in memory. The only thing that is happening is that it is drawing on the screen. So, when you go to save the image in the PictureBox, none of the user-made rectangles will be saved with it.
What you will need to do is have two different Bitmaps...one for the background image (the image loaded from file) and one for the user-drawn rectangles and such. Then, whenever you paint, first paint the rectangles to the user-drawn bitmap. Then, you can use the graphics provided by the paint to draw it.
Then, when you go to save, you have to draw the user-drawn bitmap over the background image.
Here's some pseudo-code and real code:
Image backgroundImage;
Bitmap userDraw;
Rectangle rect;
private void OpenMenuItem()
{
backgroundImage = new Image(file);
pictureBox1.Image = backgroundImage;
pictureBox1.Invalidate();
}
private void pictureBox_MouseDown()
{
if (userDraw == null)
{
userDraw = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
}
rect = new Rectangle(e.X, e.Y, 0, 0);
pictureBox1.Invalidate();
}
private void pictureBox_MouseMove()
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
pictureBox1.Invalidate();
}
}
private void PictureBox_Paint(object sender, PaintEventArgs e)
{
if (myBitmap == null) { return; }
using (Pen pen = new Pen(Color.Red, 2))
{
using (Graphics g = Graphics.FromImage(myBitmap))
{
g.Clear(Color.Transparent);
g.DrawRectangle(pen, rect);
}
e.Graphics.DrawRectangle(pen, rect);
}
}
private void SaveMenuItem()
{
if (myBitmap != null)
{
using (Graphics g = Graphics.FromImage(BackgroundImage))
{
g.DrawImage(myBitmap, 0, 0);
}
BackgroundImage.Save("C:\\Temp\\Bitmapsave.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
The way that you are doing it, you won't actually get what you're expecting because you're not drawing the rectangles onto any surface that will keep it in memory.
As far as running out of memory, that is something else within your program. It means that you're probably doing other things and not disposing of the memory and therefore are running out. I've tested this and I have no problems when saving an image and then opening it.
|
|
|
|
|
I have rectified the problem. Problem lies in if condition in the save tooltip. if i remove the if condition and save the file it's works.
new code
Image bac;
Bitmap myBitmap;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (mybitmap == null)
{
mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
}
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
{
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
this.Invalidate();
break;
}
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mybitmap == null)
{
return;
}
using (Pen pen = new Pen(Color.Red, 2))
{
using (Graphics g = Graphics.FromImage(mybitmap))
{
e.Graphics.DrawRectangle(pen, rect);
if (label1.TextAlign == ContentAlignment.TopLeft)
{
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
mybitmap.PixelFormat);
}
else if (label1.TextAlign == ContentAlignment.TopCenter)
{
SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
float left = ((float)this.Width + label1.Left) / 2 - size.Width / 2;
RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
}
else
{
SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
float left = (float)label1.Width - size.Width + label1.Left;
RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
}
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (mybitmap != null)
{
SaveFileDialog SaveFD1 = new SaveFileDialog();
SaveFD1.FileName = "";
SaveFD1.InitialDirectory = "C";
SaveFD1.Title = "save file Name";
SaveFD1.Filter = "JPG|*.jpg|Bmp|*.bmp";
if (SaveFD1.ShowDialog() == DialogResult.OK)
{
System.IO.Stream filename = (System.IO.FileStream)SaveFD1.OpenFile();
int r = SaveFD1.FileName.Length;
for (int r1 = 0; r1<=r;)
{
if (SaveFD1.FileName[r1] != '.')
r1++;
else
{
r = r1;
break;
}
}
if (SaveFD1.FileName[++r] == 'j')
{
using (Graphics g = Graphics.FromImage(bac))
{
g.DrawImage(mybitmap, 0, 0);
}
bac.Save(filename, ImageFormat.Jpeg);
}
else if (SaveFD1.FileName[r] == 'b')
{
using (Graphics g = Graphics.FromImage(bac))
{
g.DrawImage(mybitmap, 0, 0);
}
bac.Save(filename, ImageFormat.Jpeg);
}
else
{
using (Graphics g = Graphics.FromImage(bac))
{
g.DrawImage(mybitmap, 0, 0);
}
bac.Save(filename, ImageFormat.Png);
}
filename.Close();
}
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFD.FileName = "";
OpenFD.Title = "open image";
OpenFD.InitialDirectory = "C";
OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
if (OpenFD.ShowDialog() == DialogResult.OK)
{
file = OpenFD.FileName;
bac = Image.FromFile(file);
pictureBox1.Image = bac;
pictureBox1.Invalidate();
}
}
Now the above code works as expected.
Now can i clear the previous work done.
Ex: I have drawn the rectangle but i don't want this so i am drawing new rectangle (note: the above code will make invisible the previous rectangle when new rectangle been drawn) and saved the file. when i open the saved file its show all the rectangle drawn.
How can i clear the previous work (drawn)?
Thanks
|
|
|
|
|
sorry, have disabled this (g.Clear(Color.Transparent);) functions.
Thanks "William Winner" for guiding me....
modified on Friday, May 21, 2010 3:00 AM
|
|
|
|
|
How can I make my service to start certain application, so user'll be able to use it?
|
|
|
|
|
Process.Start can start applications. But a service starting applications doesn't looks good. Services are not meant for this.
Best wishes,
Navaneeth
|
|
|
|
|
unfortunately if I use Process.Start applications run without any interface
|
|
|
|
|
If I'm not mistaken, and I could be, you have to check the "Allow service to interact with desktop" option under the "Log On" tab of the service properties.
|
|
|
|
|
Sure, but I'd like to start applications on the users desktop
|
|
|
|
|
Services run under a non-visible desktop. What you want to do shouldn't be done at all. The "Allow the service to interact with Desktop..." option is for the service to put up a small user interface, like notifications or options dialogs. It is NOT going to allow the service to launch an application on the users Desktop. Any process launched by the service will inherit the session and desktop of the process that launched it, i.e.: your service.
There's just about never a good reason to do this and I suspect you're approaching your design all wrong.
For instance, what happens when your service needs to launch the UI app and there's noone logged in?
|
|
|
|
|
A windows service should not be used to start applications for users. However, if yu really want to do this, the service should wait for a user to log in, and then launch the app. You'll have to turn on "Interacts with desktop" in the service.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
That is not a good idea -- rethink the problem.
|
|
|
|
|
Thank you all! I realize that it was bad idea and switch to autorun application without UI.
|
|
|
|
|
Is there any way that i can make clone of hard drive c to mu external one by using c# code?
can anyone help me with guideline or opensource software which has the desirable method.
|
|
|
|
|
Ummm, maybe a something like, File.Copy ?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I wouldn't attempt it with managed code.
|
|
|
|
|
I'm asking how to do it not should I do it.
can anyone please provide me with any clue
|
|
|
|
|
Beside using File.Copy (as mentioned) use methods like Directory.GetDirectories and Directory.GetFiles . Maybe recursive programming will help you out as well..
|
|
|
|
|