I have a .jpg image which I display in a PictureBox like so: -
-
// Members of this form
-
private Bitmap bm;
-
private string imagePath = "file.jpg";
-
-
// Inside constructor of form
-
bm = new Bitmap(imagePath);
-
pbDemo.Image = _bm;
-
This works as expected.
Now, inside the pbDemo_Paint event I use a Graphics object to draw lines and text onto the image like so: -
e.Graphics.DrawString("abc", myFont, Brushes.Black, pt);
-
This works too, only using the parameter e of type PaintEventArgs.
Here is where I need some guidance.
After I have added some text/lines I want to save this to a new .jpg image. Here is what I have (inside a button click event): -
Bitmap tempBitmap = new Bitmap(pbDemo.Width, pbDemo.Height);
-
tempBitmap = bm;
-
tempBitmap.Save("newfile.jpg");
-
However this only saves the original image I loaded, not the newly added text or lines.
What am I doing wrong?
Thanks,
Andrew
Lets take a look at what you're doing, then examine how it differs from what you want.
* You load an image, A
* You create a copy of A called B
* You assign the PictureBox to show A
* Every time the PictureBox paint event occurs, you modify B
* You save B
I've bolded the important ones here. You're displaying A but you're modifying B, so your PictureBox will never show any changes you make to B. Additionally, you're doing these modifications in the paint event of the PictureBox, which means you're making the change over and over again. You don't need to do this, you only need to make the change once since you're modifying an image.
You need to make your changes to the modified image at some point (constructor, in response to an event, whatever), then assign the PictureBox to just draw that image.
Finally, I'm not sure why the modified image never showed your changes. I tested this before I posted and it was working... unless your paint method never fired. Do a quick test yourself, create the graphics in the constructor and modify it, then save it. You should see the changes in the file you save.
5 8855
When you draw, you're drawing on the PictureBox itself. When you save, you're saving the original image you loaded, which contains none of the modifications you made on the PictureBox.
There's a method on the Graphics class called FromImage which lets you create a Graphics object that renders to an image. You can do something like the following... - Image baseImage = Image.FromFile("something.jpg");
-
Image modifiedImage = (Image)baseImage.Clone();
-
Graphics g = Graphics.FromImage(modifiedImage);
-
g.DrawString("BLAH", new Font("Courier New", 10.0f, FontStyle.Bold), Brushes.Black, 0, 0);
-
-
modifiedImage.Save("text-image.jpg");
Thanks for your fast reply.
So when I open the file I do your first and second line, right?
And in my button event for saving I do the second 2 lines?
It depends on your program. I just did the above in the constructor of a windows form as a test. I don't know the particulars of your program so I can't really say, but in general you can do the first two whenever you like. If you intend to modify the images elsewhere, I'd recommend making them member variables. If you want to modify it in response to some event, like your button click, you can put the modification there.
You don't need to modify your PictureBox at all, just the image it draws. The only reason I created a clone was because I didn't want to lose the original. If you don't care about this, you don't need to do it.
Hopefully that helps.
I did as I suggested above and the lines using the g object do not seem to be registering - the image does not contain the lines/text I add.
My constructor looks like this: -
private void ResizeAndDisplayImage()
-
{
-
baseImage = Image.FromFile(imagePath);
-
modifiedImage = (Image)baseImage.Clone();
-
pbDemo.Image = baseImage;
-
pbDemo.SizeMode = PictureBoxSizeMode.StretchImage;
-
}
-
My pbDemo_Paint event looks like: -
private void pbDemo_Paint(object sender, PaintEventArgs e)
-
{
-
Graphics g = Graphics.FromImage(modifiedImage);
-
g.DrawString("1", myFont, Brushes.Black, folioOnePoint);
-
g.DrawLine(myPen, 501, 206, 501, 199);
-
g.DrawLine(myPen, 501, 199, 515, 199);
-
g.DrawLine(myPen, 515, 199, 523, 212);
-
g.DrawLine(myPen, 523, 212, 523, 245);
-
}
-
And my button to save is: -
private void btnSaveImage_Click(object sender, EventArgs e)
-
{
-
modifiedImage.Save("new-file.jpg");
-
}
-
After the paint event fires the text I add and the lines are nowhere to be seen. After I save the new-file.jpg is just the same as the original.
Lets take a look at what you're doing, then examine how it differs from what you want.
* You load an image, A
* You create a copy of A called B
* You assign the PictureBox to show A
* Every time the PictureBox paint event occurs, you modify B
* You save B
I've bolded the important ones here. You're displaying A but you're modifying B, so your PictureBox will never show any changes you make to B. Additionally, you're doing these modifications in the paint event of the PictureBox, which means you're making the change over and over again. You don't need to do this, you only need to make the change once since you're modifying an image.
You need to make your changes to the modified image at some point (constructor, in response to an event, whatever), then assign the PictureBox to just draw that image.
Finally, I'm not sure why the modified image never showed your changes. I tested this before I posted and it was working... unless your paint method never fired. Do a quick test yourself, create the graphics in the constructor and modify it, then save it. You should see the changes in the file you save.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Arches38 |
last post by:
Hello,
I need to put some text informations on an image and then save the
image (with informations on it) in a BMP file.
I work with two classes :
- a class herited of a CDocument object
- a...
|
by: Anand Ganesh |
last post by:
HI All,
I have an Image. I want to clip a portion of it and copy to another image.
How to do this? I know the bounding rectangle to clip.
Any suggestions please.
Thanks for your time and...
|
by: kieran |
last post by:
Hi,
I want to download an image from the web and save it locally. I have
spent all day messing about with this and am still no where. We have
a firewall so i use the below code. I know it...
|
by: Rob Willaar |
last post by:
Hi All,
I try to draw text on a picturebox.
The text appears but it is drawn on top of the picture.
when i try to save the result the text is gone.
|
by: myjpa1 |
last post by:
Hi, all,
I want to draw image directly onto top of screen, which simulates the
OSD(on screen display) effects. However, class Graphics must link to
certain controls, that cannot be used to draw...
|
by: benfly08 |
last post by:
Hi, guys.
I have a program to draw bar/pie chart based on the data i hard coded in it. However, my image comes with "BLACK" background color. I don't know how to fix this. The code snippet is...
|
by: klaydze |
last post by:
hi to all the programmers out there. can u help me how to upload an image then get the path of the image and then save the path in database.any one can give me some line of codes or idea how to do...
|
by: patr0805 |
last post by:
Hi I am not new to vb, but I still can't solve this problem. I have flipped the image in the form, but when I save the image then the saved image is the original unflipped one)
Private Sub...
|
by: Ade Seriki |
last post by:
I need assistence on how to load an image and save same into my database.
|
by: AndrewBuchan |
last post by:
Thank you for your patience Gary, I followed your advice and have got this functioning.
Just a side note, if you do not mind.
The text and lines I am adding are position specific - that is to...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| | |