Embedded resource and adding images - image

I am developing a form application with monodevelop. I'm using gtk#.
I need to add an image to a widget in the form. I am also trying to make this image an embedded resource as well, before including it in the form.
so far this is what I have:
HBox CharacterPic = new HBox();
Image LegionnairePic = new Image('somehow load the embedded resource image here');
CharacterPic.PackStart (LegionnairePic);
In the 'Solution' section to the left, I have added .jpeg files and changed their 'build action' to 'embedded resource'. However, I cannot access/load them onto the form them as so:
Image LegionnairePic = new Image(<namespace>.<resource>);
How do I add the image resource to the form? Am I even adding the resource correctly?

I believe you have to access the embedded files with the following method:
// string resource_id is the Resource ID of the file in the sidebar “Properties”
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_id);
This returns the resource file as a Stream.
If this helped, thank that guy like I did :)

Related

Syncfusion Xamarin 'Value cannot be null. Parameter name: stream' during adding image

I would like to load image to pdf from stream. When I try to do that I get an error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: stream'
Here is my code
Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("MyApp.Resources.drawable.image.png");
PdfImage image = PdfImage.FromStream(imageStream); <---- here is error
g.DrawImage(image, new PointF(20, 20));
Firstly , in this way you need to put the image in Forms project instead of Android project .In my demo I put the image in the folder Assets . And the code should like the following
var assembly = this.GetType().GetTypeInfo().Assembly;
Stream s = assembly.GetManifestResourceStream("MyApp.Assets.icon.png");
In addition , make sure that the build action of the image is Embeddded Resource
Right Click the image -> Properties
We have checked the issue with loading the image to pdf from stream, but it is working properly. And the images are loaded and insert into the Pdf document and make sure that the build action of the image is Embeddded Resource. Kindly please try the sample in this below documentation and let us know the result.
KB : https://www.syncfusion.com/kb/10375/how-to-insert-an-image-into-pdf-in-xamarin
UG : https://help.syncfusion.com/file-formats/pdf/working-with-images
Note: I am work for Syncfusion.

How to verify that Android Layout Resource Xml exists in Xamarin?

I have a layout in my Xamarin Android project. I want to confirm that the resource is actually present in build.
I tried the following code:
var layout = Resources.GetLayout(Resource.Layout.my_xml_resource);
var xml = layout.ReadInnerXml();
System.Diagnostics.Debug.WriteLine(xml);
The GetLayout call does not throw the NotFoundException so presumably the resource exists. However, layout object, upon inspection, displays None.
The xml variable is empty and all attempts to read the xml are unsuccessful.
I am down this rabbit hole, because I am trying to use the layout with Inflate. Unfortunately, the output of inflate does not have the child controls I would expect and I suspect the resource layout is empty.
nativeView = inflater.Inflate(Resource.Layout.my_xml_resource, view, true);
What am I missing? Is there another way to verify the resource exists?
Use the Resources.GetIdentifier(String, String, String) Method could verify that Android Layout Resource Xml exists or not.
In C#, you could try the code below. If the resId not be 0, the resource exists.
int resId = Resources.GetIdentifier("textlayout", "layout", "com.companyname.app1");
textlayout: This is the layout name of my project, please note it need lower case.
layout: The type of resource you want to verify. It need lower case as well.
com.companyname.app1: PackageName, you could get from the AndroidManifest.xml file.

Load a PNG image file for display on a Vaadin Flow layout as a Image object

Vaadin Flow offers an Image class, representing an HTML img tag.
If I have a file named logo.png stored in the resources folder of my Vaadin 12 app, how do I load that file for display as an Image on a layout?
The example for Image in the Vaadin 8 Sampler shows code no longer relevant as I cannot find a ClassResource class in Flow.
You can put that logo under what your build tool/setup considers a root for web resources under the directory frontend/... and then reference that resource like so:
new Image("frontend/images/logo.png", "Acme Inc. Logo")
If your resource is not located inside the root for web resources, this doc page gives a hint on using a com.vaadin.flow.server.StreamResource to provide the data by an java.io.InputStream:
StreamResource res = new StreamResource("logo-image.png", () -> {
// eg. load image data from classpath (src/main/resources/images/image.png)
return MainView.class.getClassLoader().getResourceAsStream("images/image.png");
});
Image imageFromStream = new Image( res,"Alternativ text description for logo image");
add(imageFromStream);
In my case the problem was in the location of the file:
If you use: new Image("img/logo.png", "Logo") then the file location should be /src/main/resources/META-INF/resources/img/logo.png in Spring Boot project

Adding Image into Bing map

I've tried adding a image via the following however it is still not working. The image type is a content.
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));
//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;
// Center the image around the location specified
//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);
mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);
Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources
This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.
I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.
I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?
Assuming that you have added it directly:
If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:
Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
However if you have set the "Build Action" to "Content" you should use the GetContentStream method
Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root):
/pages - Contains the actual pages and therefore also the page containing the above code
/images - Contains the actual PNG images that have to be referenced
In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows:
1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level)
2. Now reference /images -> ../images
3. Now add the actual file reference -> ../images/myimage.png
If you use the correct URI the problem is solved.
The main question seems to be how to get true uri.
For me, the following table helps me in this case (I only have it in German):
http://msdn.microsoft.com/de-de/library/aa970069.aspx
example:
// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
example 2:
Uri uri = new Uri("pack://application:,,,/File.xaml");
or Codebehind:
'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
/projectname;component/images/menu/lost.png
Is the correct way, the rest of your answer is really not working

How to use System.Drawing.Image in RDLC Image Control?

Is it possible to use System.Drawing.Image in an RDLC Image Control?
All I have been reading were 3 methods:
database
embeded resource
external file
Thank you thank you.
EDIT:
Following up from this .NET or C# library for CGM (Computer Graphics Metafile) format? I now got the image in System.Drawing.Image format and want to display it as part of the report (as an image) --- that's what I want to do.
Not sure if this is what you are looking for, but if you have an image in code and you want to show it in the report, create a wrapper object that has a property that returns the image as a byte array and give then an instance of this wrapper-class with the valid image to the report as a ReportDataSource.
Something like:
ReportDataSource logoDataSource = new ReportDataSource();
logoDataSource.Name = "LogoDS";
logoDataSource.Value = new List<LogoWrapper>() { yourLogoWrapper };
localReport.DataSources.Add(logoDS);
In the report you then you can the image as it were from the database
=First(Fields!LogoByteArrayProperty.Value, "LogoDS")
The wrapper looks something like:
class LogoWrapper{
...
public byte[] LogoByteArrayProperty{
get{
// Return here the image data
}
}
}
I use this quite often. It has the advantage that I don't have to add the image to the db or add it as a resource of every report. And furthermore, the app can say which image should be used.
Please note, the given image format must be known from the rdlc-engine.
The last question would be, how to convert a system.drawing.image to a byte array. I work with WPF and therefore, I dont known. But I'm sure google will respond to this question very reliable.
You Can use the 'Database' Source Option along with Parameters to Dynamically set Image Source from Byte Arrays.
Code Behind:
var param2 = new ReportParameter()
{
Name = "CompanyLogo",
Values = { Convert.ToBase64String(*ByteArrayImageObject*) }
};
ReportViewer1.LocalReport.SetParameters(param2);
rdlc File:
1- Add Text Parameters 'CompanyLogo' and 'MIMEType'
2- Set the Value Property of the Image to =System.Convert.FromBase64String(Parameters!CompanyLogo.Value)
3- Set MIME Type Property to
=Parameters!MIMEType.Value
4- Use 'Database' As Source
How can I render a PNG image (as a memory stream) onto a .NET ReportViewer report surface
i am not quite sure what do you want to do with this but in general it is not possible.Image Control is just a image holder in the RDLC files.These 3 options specify the location from where the image control takes the image which to display from- database, embeded resource or external file. If you give me more info on what do you want to achieve i can give you some kind of solution.
Best Regards,
Iordan

Resources