 |
|
 |
Hi everyone,
Context:
1) ASP.NET
2) JPEG Image
What i want:
1) Write text on the bottom of Image
2) Auto fit text/font size by width of image
I'm using next Overlay call:
string path = Server.MapPath(@"\Images\vsimage.jpg");
int size = 10;
BitMap img2 = new Bitmap(path);
Bitmap img = Overlay.TextOverlay(img2, "www.mysite.com", new Font("Verdana", size,FontStyle.Bold), Color.Black,
true, false, ContentAlignment.BottomCenter, 0.2F);
First question is why output text size every time the same when different FontSize passed,
second question is why text is wrapped by two lines and not displayed in one line, i see that there is a lot of space from left side and right but text wrapped...
Thanks
|
|
|
|
 |
|
 |
Which image are you looking at? img or img2? img is the modified bitmap.
It also appears you are using this in a website, correct? If so you likely need to save the modified image before showing in a webpage or stream the bits out dynamically (more advanced, google for examples). Also note both IIS and the client browser will aggressively cache any images, so you may want to preprocess all the images and save them as different filenames (e.g. \Images\vsimage.watermarked.jpg). Give that a shot and let everyone know how it works out.
img.Save(path.Replace(".jpg", ".watermark.jpg"))
|
|
|
|
 |
|
 |
Thanks a lot for sharing this code.
I have modified this to take an TIF image also and overlay. I am encountered a problem with when I save overlaied image in TIF format, it autometically changes the DPI to output image. For example if I am talking an 200 DIP image and have overlay done on it and then try to save image, it will autometically change the DPI to 96.
Regards,
-Anurag.
|
|
|
|
 |
|
 |
hi
Is there a way to overlay a smaller image on a larger image at a specfic position??
Thanks
|
|
|
|
 |
|
 |
Sure. Take a look at the methods on Graphic. You can draw lines, curves, text, images, .. lots of fun stuff!
|
|
|
|
 |
|
 |
Any insight on making this support carriage returns inside the overlay text? If I place \r\n into the mix, the calculations are thrown off.
|
|
|
|
 |
|
 |
If you are using the demo, just press [CTRL]-Enter to break to a new line. If you are using the code, use the C# version .. \r\n is just characters in VB. For VB (or C#), you can use Environment.NewLine.
|
|
|
|
 |
|
 |
First of all thank for a great job.
Is it possible to move text anywhere in the image through the mouse.
If yes can you tell me how?
Thank you.
Rajesh.
|
|
|
|
 |
|
 |
Thanks, Rajesh.
It would be possible to move the text with the mouse, but it would require a lot of coding. The demo app was only meant to show how the function operates, not as a front-end for creating graphics. If you want to try, start with the MouseDown and MouseMove events. On second thought, it might be easiest to use a draggable transparent label (or panel) control sitting on top of the graphic ...
However, for your purposes, you may want to just use a graphics program. There are many free ones available
HTH
|
|
|
|
 |
|
 |
I think that code is great. However, I'm looking for I way to write overlay text outside the program Window. For exemple, I'd want to tell that small program to write Hell World in a specific coordinate of my Internet Explorer window, which is opened besides of my program. I'm coding in C#. I guess The Code Project have already covered that subject but I can't find any specific information. Can someone help me with that?
Thanks in advance.
|
|
|
|
 |
|
 |
Hell World?
Just a quick search reveals a few articles that might help you out. Haven't tested these, just the first ones found.
Locus Effects[^] anchor-based "look-here" techniques, plus you'll need the hDC-based painting.
API hooking revealed[^] hooking into external procs.
Of course, there's more than one way to skin a cat. You may want to host the webpage in your app to have more control: Hosting a webpage inside a Windows Form[^]
HTH
GW
|
|
|
|
 |
|
 |
I am just looking for this to do a dual screen Bible presentation program. May God bless you.
Is it easy to add tab (\t) and newline (\n) support?
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.
|
|
|
|
 |
|
 |
Paul,
The Overlay method will use newlines if they are in the text sent. Just change the textbox in the demo to be multiline to test. If, however, you want the text to only break at your newlines, you need to add a flag to the following line:
strFormat.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap; // add NoWrap to preserve layout.
You'll also need to widen the initial rectangle, or you can end up clipping (even with the NoClip flag).
Tabs, unfortunately, are not as easily supported. If the tabs are always at the beginning of a line (like paragraph indention), then you can just replace them with 3 or 4 spaces to simulate a tab.
What you're *supposed* to do is use the SetTabStops() method:
strFormat.SetTabStops(0F, new float[] {60F});
However, you may need to play with the values and maybe even base them off the calculated font size to get good results. In fact, until I added NoWrap, I couldn't get the tabs to show at all, and afterwards, the values I used didn't seem to have any effect. There's not a lot of info on SetTabStops().
HTH
|
|
|
|
 |
|
 |
Thanks for the support. I think the space for the tab will do.
Best regards,
Paul.
Jesus Christ is LOVE! Please tell somebody.
|
|
|
|
 |
|
 |
You can set TabStops easily. In my program, I needed to render Text like a Document. So, I set the GraphicsMode to Document in my graphics object and used this for the TabStops... My Tabs are about a 1/2 inch apart.
sFormat.SetTabStops(0.0f, new Single[]{(float)(300.0 * 0.55)});
|
|
|
|
 |
|
 |
nice article!
thanks and happy new year
|
|
|
|
 |
|
 |
Great job on the example but..... well OK it was a great job regardless. Only thing is I really need to do non-destructive text annotations in an image. Anyone know how to do this?
|
|
|
|
 |
|
 |
I'm not sure what you mean by "non-destructive" ... in a sense, the function is non-destructive, because it uses a copy of the original to annotate. The image you pass is not changed; you get a new image back.
But I imagine what you are talking about is more like a "layered" image (the way PhotoShop works). You could have the original on the bottom and the text on a transparent layer above. There is one "standard" image format I'm aware of that supports layers -- TIFF, but it's not a widely used format.
However, if you're looking for something like a marquee with scrolling text and a background image, the best bet (other than finding one already made) would be to inherit from a control (like a Panel) and override OnPaint().
GW
|
|
|
|
 |
|
 |
Hi there...
I’ve a problem to add code on load images from hard disk. I’ve try many times to add that code, but it’s still don’t work. The text doesn't appear on image. So, can you please help me? thanks..
|
|
|
|
 |
|
 |
Bariah,
The demo code stores the original image in a form variable m_OrigImage so that when you change the parameters or text it will overlay onto a "fresh" (clean) image.
To use your own image, you just need to load it and set m_OrigImage to your image (for example, m_OrigImage = Image.FromFile(filename)). I'll update the demo so it can load an image, but it may be a couple of days before the update gets posted.
If using the function in another project, you would load your image and pass it to the Overlay( ) function, which returns the modified image.
GWSyZyGy
|
|
|
|
 |
|
 |
Hi
I have add in some code to save the image as jpg file and open the jpg file in IE. I found that image display in IE quite dirty, noise?
Any solution to make it nicely?
Thank Alot
Cliven Yong
|
|
|
|
 |
|
 |
Well, not knowing what your code looks like, I'd have to guess the quality parameter you used is too low. JPG is a lossy format, and the .NET jpg encoder is somewhat ... lacking.
If you want to preserve the information, I would suggest saving as either bmp or png. Then you can convert to jpg in something like PhotoShop (that will do a much better job).
Here's some code that will let you do that:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim bmp As Bitmap = New Bitmap(pbOverlay.Image)
Dim dlg As New SaveFileDialog()
dlg.AddExtension = True
dlg.CheckPathExists = True
dlg.CreatePrompt = False
dlg.OverwritePrompt = True
dlg.RestoreDirectory = False
dlg.ValidateNames = True
dlg.FileName = "Overlay"
dlg.Filter = "Jpeg Image (*.jpg)|*.jpg|PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp"
dlg.FilterIndex = 0
If dlg.ShowDialog(Me) = DialogResult.OK Then
Select Case dlg.FilterIndex Case 1 Dim iQual As Integer = -1
Dim sQual As String = "90"
While (iQual < 0 Or iQual > 100)
sQual = InputBox("Enter jpg quality (0-100)", "JPG Quality", sQual)
Try
iQual = Integer.Parse(sQual)
Catch
iQual = -1
End Try
End While
Dim eps As EncoderParameters = New EncoderParameters(1)
eps.Param(0) = New EncoderParameter(Encoder.Quality, iQual)
Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")
bmp.Save(dlg.FileName, ici, eps)
Case 2 Dim ici As ImageCodecInfo = GetEncoderInfo("image/png")
bmp.Save(dlg.FileName, ImageFormat.Png)
Case 3 bmp.Save(dlg.FileName, ImageFormat.Bmp)
Case Else
MsgBox("Unrecognized selection!", MsgBoxStyle.Exclamation)
End Select
dlg.Dispose()
End If
End Sub
Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders As ImageCodecInfo()
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length
If encoders(j).MimeType = mimeType Then
Return encoders(j)
End If
Next j
Return Nothing
End Function
|
|
|
|
 |
|
 |
Dear GWSyZyGy
Excellent. Now the image is clear and without noise.
Thank a lot.
Cliven
|
|
|
|
 |
|
 |
Hi.
I'm sure you already know this, but to get rid of the jagged edges you can use the following method.
Let g be the Graphic object.
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
This heavily reduces or eliminates the jagged edges. (Obviously with a slight performance hit, but for this kind of drawing it's worth it.)
Great example overall.
Keep it up.
/ Thomas
Wit is educated insolence.
|
|
|
|
 |
|
 |
Hi,
Nice little article!
I've developed a VB.Net utility which uses Photoshop scripting to create a new set of JPG images from an existing set, allowing the user to resize, add saturation, define the output image quality and a bunch of other Photoshop stuff. It even uses PS7's own JPG optimisation to create *really* good images... I use this utilty to prepare compressed images and thumbnails for my website. (I'll post it on my site one of these days so everyone can get at it but it still has a bug or two!)
Anyway, the only other thing I've wanted to add is to allow the user to specify a copyright message or other text on the image. This would most likely be in one corner in a very small font. So far I've not had much luck doing this using PS7 scripting as it is difficult to do the positioning accurately for the same reasons you mention in your article. So, I'd like to see what you've got above extended so the text could be placed in any corner, not just in the center. But you've given me somewhere to start... thanks!
Mike.
|
|
|
|
 |