I'm working on an older project, updating it. Part of the program has a toolstrip with many buttons on it, each with an image. I found that the images are stored in a Base64 encoded imagestream in the resx for the form and accessed as such:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
...
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
...
this.toolStrip1.ImageList = this.imageList1;
...
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
...
this.toolStripButton1.ImageIndex = 0; //There are 41 images, so this can be between 0 and 40
I need to add another button with a new image. How can I add an image to this stream?
I cannot use the designer, as it crashes as soon as I load the form (I believe because it uses a custom component with unsafe code).
I could always just add a new image resource separate from the stream, but that would make that one button different and thus it would create problems with consistency, causing upkeep problems later. So I'm wondering if there is any way for me to edit the imagestream. I can access the raw base64 string, but I have no idea where to go from here.
Create another form.
Add an ImageList component.
Add an arbitrary image in order to generate an "imagestream".
Open old resx and copy the "value" element.
Open new resx and paste value element.
Open new form.
Add images as needed.
Save new form.
Open new form's resx file.
Copy value element.
Open old form's resx file.
Paste in new value element.
I found a way to do this using code:
imageList1.Images.Add( NEWIMAGE );
ResXResourceWriter writer = new ResXResourceWriter("newresource.resx");
writer.AddResource("imageList1.ImageStream",imageList1.ImageStream);
writer.Generate();
writer.Close();
writer.Dispose();
The code will write the updated ImageStream to the new resource file. I can then copy it to my current resource file.
Related
I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);
I have a directory with .png images which I display in a third party combobox of my c# program. So the user is able to choose one of this images using the combo box. Basic code used:
Bitmap thump = new Bitmap(<path>);
ComboItem item = new ComboItem();
item.Image = thump;
MyComboBox.Items.Add(item);
Now I would like to update one of this images at runtime. Unfortunately I can't delete the old image because it is still opened in my program, so somehow I either need to close it or open it in a way that does not keep the image in use by my program. The changes to the bitmap are not done in my program, I just pass the path of the dirctory to another program which saves the bitmap there (but fails at the moment because it can't delete the old bitmap).
I guess this is a simple problem but I could not find a solution here or on the internet.
First read the file to memory, then create the Bitmap using that data.
var m = new MemoryStream(File.ReadAllBytes(filename));
Bitmap thump = (Bitmap)Bitmap.FromStream(m);
Now, i developing a WinForms application. I need to create a custom control. My custom control uses a pictures. I don't want store these pictures in project resources file.
global::Wrigley_Data_Mart_Manager_2.Properties.Resources.previous_16x16;
I want to store pictures in resources file of custom control. But it's very uncomfortable. (designer always delete pictures from there). Where i can store image for my custom control? I want to my custom control was self-sufficient and that it can be used in other projects.
If you don't want to use a Resource (resx) then you could always include the image file within the project as an embedded resource.
You would create a folder to hold the image(s), and invoke "Add Existing", adding the images you'd like to embed into the project. Then in the Properties window for each file, for the "Build Action" selection, highlight the "Embedded Resource" option.
To access that file, you'd call Assembly.GetManifestResourceStream(string) with the full namespace of the project + the folder path within the project:
For Example, with Project Name: "MyCompany.MyTestProject" and you store the image(s) in the "Images" folder as "Images/image1.gif",
The namespace would then be:
Assembly.GetManifestResourceStream("MyCompany.MyTestProject.Images.image1.gif")
An alternative could be converting them to byte arrays and storing them as BLOBs in your database.
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
Then convert them back:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
I have a C# form that I am creating completely manually (i.e. without using the Visual Studio Design View). In this form I have a Label created (simply) as follows:
private Label myLabel = new Label();
Later on in the code, during a callback, I want to set an image for this label. I have found one way to do it and here is the code:
myLabel.Image = Image.FromFile("Images\\mark.png");
This works but the problem is, when I distribute my application I am going to always have to provide a folder (Images) containing mark.png.
This is acceptable I guess but it's really not what I want...
The interesting thing is, I looked at some other code I created where I used the Visual Studio Design View to add an image to a label and the Designer does it totally different.
It somehow parses the image, figures out the binary, and saves the base64 representation of this binary as a string in the .resx file:
<data name="fileDisconnect.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABkAAAAXCAYAAAD+4+QTAAAABGdBTUEAAK/INwWK6QAAAAlwSFlzAAAO
vAAADrwBlbxySQAAAA10RVh0U291cmNlAE51dm9sYaxPNfEAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5F
...
...
ns2di2Jn545YY+PJHw3ZbmREJBgZLDwzgehomExoMpmUG1spVLuhAG/80RSKwyEGo7ZBJNKcsrf/Pd7Q
MOijAUML48mPeAPCJ5FKhMcSBOt9Ach5ThKVujqeQvEa8v8FrglBl5d/7zYAAAAASUVORK5CYII=
</value>
Looking at the Designer file source code you can see it sets the image lable as follows:
this.fileConnect.Image = ((System.Drawing.Image)(resources.GetObject("fileConnect.Image")));
So, as you can see, doing it this way is much more elegant. There is no need to actually save the image to your hard disk and provide the image when delivering your application. The image information is stored in the .resx file and images are set getting the binary content programatically from the .resx.
My question is.... If I am creating my control manually how can I get the same kind of behavior? Can I create a .resx file, open the image with a HEX editor and copy the binary string into the .resx file? Then, when adding the image to the label I could just just do it like above.
If you save the image in Resources, you can just do this:
Label myLabel = new Label();
myLabel.Text = "whatever";
myLabel.Image = Properties.Resources.MyLabelImage;
this.Controls.Add(myLabel);
In a GridView, I've templated the items so an Image control inside that template receives an Uri using a binding (also tried with BitmapImage) to show a picture file. It works, but I cannot delete those files because those files are blocked. Even if I clear the collection feeding the GridView.ItemsSource, and even if I call GC.Collect(), they are still blocked.
Thanks to Philip for his working suggestion... as I used a Converter to feed a BitmapImage to every Image control, found some particular things had to do to make it work, and here it is for future reference:
InMemoryRandomAccessStream Ras = new InMemoryRandomAccessStream();
var archivo = CartoonsDownloader.FolderImagenes.GetFileAsync(TheFileName);
var fileStream = CartoonsDownloader.FolderImagenes.OpenStreamForReadAsync(TheFileName);
fileStream.Result.CopyTo(Ras.AsStreamForWrite());
BitmapImage MapaDeBits = new BitmapImage();
// Even it's RANDOM, I have to manually "Seek" it at 0.
Ras.Seek(0);
MapaDeBits.SetSource(Ras);
MapaDeBits.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
return MapaDeBits;
This could be a bug in the XAML stack (so you could report it on MSDN forums), or just a side effect of its nature of caching images. As an alternative - you can open the file yourself and set the image using the SetSource method. Then you should have better control over the file access.