How to set background image to shell or composite in SWT - eclipse-plugin

public class createShell{
System.out.println("Inside shell create");
display = new Display();
shell = new Shell(display);
shell.setSize(990, 590);
shell.setLayout(new GridLayout());
Composite comp = new Composite(shell, SWT.NO_FOCUS);
comp.setBounds(10, 10, 720, 400);
}
I have existing code like this. Need to set a background image to shell. How to give relative path of image.
The folder structure of plug-in(com.vcc.rac.ks5lmpp) is as per image.path The code file is in package inside src folder(plugins\com.rac.ks5lmpp\src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\createShell.java)
There exists a image folder(plugins\com.rac.ks5lmpp\src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\images) in which the image is there which I want to pass as background.
Image size is of 676X324 of PNG type.
Thanks in advance.

You use FileLocator in an Eclipse plug-in to find resources in the plug-in.
Bundle bundle = FrameworkUtil.getBundle(getClass()); // Or some other way to find the current bundle
URL url = FileLocator.find(bundle, new Path("path within the plugin"));
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
Image image = desc.createImage();
shell.setBackgroundImage(image);
Note: You should arrange to dispose of the image when the shell closes.
Be sure that the build.properties for the plug-in includes the images.
FileLocator is org.eclipse.core.runtime.FileLocator.
Path is org.eclipse.core.runtime.Path (not the java.nio.file Path)
ImageDescriptor is org.eclipse.jface.resource.ImageDescriptor

Related

I have to set a image on wizard dialogue,classpath load this image, but image not displayed properly

I have to set a image on wizard dialogue in swt,classpath load this image but image not displayed properly. In my design create a composit in this composit create a lebel.there after in this lebel I want add an image.After that i will create a runnable jar.This jar will be executed with proper image. In my implementation part i have create a resources (src folder) within src folder and in this resources folder kept image which i need.Please find the below code,
File file = new File("resources/Automatics_Logo.png");
Image image = new Image(Display.getDefault(), file.getPath());
Label lblNewLabel_4 = new Label(composite, SWT.NONE);
lblNewLabel_4.setImage(SWTResourceManager.getImage(CheckSystemConfigurationAndInstallation.class,image.toString()));
Image imgSWT=null; // Image class is the SWT Image class
ImageDescriptor imgDesc=null;
java.net.URL imgURL = IntroductionPage.class.getResource("/Automatics_Logo.png");
if (imgURL != null) {
imgDesc = ImageDescriptor.createFromURL(imgURL);
imgSWT = imgDesc.createImage();

ExtentReports in Selenium Webdriver

I'm having an issue where ExtentReports is not showing the screenshot when the report is viewed on another machine. When saving the image into the report, I pass the absolute path of the image file. The user who wants to view the report have to copy report.html and Errorscreenshot folder to their D drive. Then only they can see the screenshot. Please suggest another way so that user can copy these files to any of their location so that screenshot can be viewed.
My code is below:
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String dest = "D:\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png";
File destination = new File("D:\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png");
FileUtils.copyFile(source, destination);
//FileUtils.copyFile(source, );
Instead of relative path. I found it is easier if the image is converted to base64 format. In that case, we only want to share .html file only.
TakesScreenshot ts = (TakesScreenshot)driver;
String dest = ts.getScreenshotAs(OutputType.BASE64);
return "data:image/jpg;base64, " + dest ;
I have used the extent report version 2.40.2 jar and have achieved sharing of my reports folder in this way. The paths are relative here and the report would render correctly with these paths. Hope this helps. I went through many links but nothing actually helped so came up with this logic.
public String capture() throws IOException {
try{
//take screenshot and save it in a file
File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//copy the file to the required path
File destinationFile = new File(System.getProperty("user.dir")+"/reports/image_" + System.currentTimeMillis()+ ".png");
FileHandler.copy(sourceFile, destinationFile);
String[] relatvePath = destinationFile.toString().split("reports");
screenshotPath = ".\\" + relatvePath[1];
}
catch(Exception e){
//if it fails to take screenshot then this block will execute
System.out.println("Failure to take screenshot "+e);
}
return screenshotPath;
}
test.log(LogStatus.FAIL, Constants.REPORT_ERROR, test.addScreenCapture(capture()) + Constants.REPORT_ERROR_STATUS);
Use relative path for your screenshots. Save your screenshots where your html file is located.
Use relative path for screenshot as below:
String path = System.getProperty("user.dir")+"\\ErrorScreenshots\\"+screenShotName+System.currentTimeMillis()+".png";
File destination = new File(path);

Create PDF file with images in WinRT

How I can create PDF files from a list of image in WinRT. I found something similar for windows phone 8 here("Converting list of images to pdf in windows phone 8") But I am looking for a solution for windows 8. If anyone of having knowledge about this please share your thoughts with me.
Try http://jspdf.com/
This should work on WinJS, but I haven't tested it. In a XAML app you can try to host a web browser control with a jsPDF-enabled page.
ComponentOne has now released the same PDF library that they had in Windows Phone for Windows Runtime. Tho it's not open source, of course.
Amyuni PDF Creator for WinRT (a commercial library) could be used for this task. You can create a new PDF file by creating a new instance of the class AmyuniPDFCreator.IacDocument, then add new pages with the method AddPage, and add pictures to each page by using the method IacPage.CreateObject.
The code in C# for adding a picture to a page will look like this:
public IacDocument CreatePDFFromImage()
{
IacDocument pdfDoc = new IacDocument();
// Set the license key
pdfDoc.SetLicenseKey("Amyuni Tech.", "07EFCD0...C4FB9CFD");
IacPage targetPage = pdfDoc.GetPage(1); // A new document will always be created with an empty page
// Adding a picture to the current page
using (Stream imgStrm = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync("pdfcreatorwinrt.png"))
{
IacObject oPic = page.CreateObject(IacObjectType.acObjectTypePicture, "MyPngPicture");
BinaryReader binReader = new BinaryReader(imgStrm);
byte[] imgData = binReader.ReadBytes((int)imgStrm.Length);
// The "ImageFileData" attribute has not been added yet to the online documentation
oPic.AttributeByName("ImageFileData").Value = imgData;
oPic.Coordinates = new Rect(100, 2000, 1200, 2000);
}
return pdfDoc;
}
Disclaimer: I currently work as a developer of the library
For an "open source" alternative it might be better for you to rely on a web service that creates the PDF file using one of the many open source libraries available.
I think this may help you if you want to convert an image (.jpg) file to a PDF file.
Its working in my lab.
string source = "image.jpg";
string destinaton = "image.pdf";
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source);
xgr.DrawImage(img, 0, 0);
doc.Save(destinaton);
doc.Close();
Thanks.

Loading images asynchronously in windows 8 metro apps

I am new to Windows 8 app development. In my app I need to display a GridView with images and title. The image URL and the title I get from the server as a XML data. The images are downloaded from the given URL and stored in a local directory. Now, when an image is downloaded I want to notify the GridView and update the particular image view with the downloaded image. I store the title and the local image URI in an ObservableCollection. The data source of the GridView is bound to this ObservableCollection, so once the XML data is downloaded I am able to update the title through the ObservableCollection. But i don't know how to update the images once they are downloaded.
Assuming that your image is saved in the local data folder (ApplicationData.Current.LocalFolder) - you can create a new BitmapImage this way:
var imagePathInLocalDataFolder = ?
var imageUri = new Uri("ms-appdata:///local/" + imagePathInLocalDataFolder, UriKind.Absolute);
var bitmapImage = new BitmapImage(new Uri(imageUri));
You can then assign the bitmapImage variable value to a property that you bind to an Image.Source - and you should see your image.

WinJS / WinRT: detect corrupt image file

I'm building a Win8/WinJS app that loads pictures from the local pictures library. Everything is generally working fine for loading valid images and displaying them in a list view.
Now I need to detect corrupt images and disable parts of the app for those images.
For example, open a text file and enter some text in it. Save the file as .jpg, which is obviously not going to be a valid jpg image. My app still loads the file because of the .jpg name, but now I need to disable certain parts of the app because the image is corrupt.
Is there a way I can check to see if a given image that I've loaded is a valid image file? To check if it's corrupt or not?
I'm using standard WinRT / WinJS objects like StorageFile, Windows.Storage.Search related objects, etc, to load up my image list based on searches for file types.
I don't need to filter out corrupt images from the search results. I just need to be able to tell if an image is corrupt after someone selects it in a ListView.
One possible solution would be to check the image's width and height properties to determine whether it is valid or not.
Yeah, the contentType property will return whatever the file extension is. The best way I can find it to look at the image properties:
file.properties.getImagePropertiesAsync()
.done(function(imageProps) {
if(imageProps.width === 0 && imageProps.height === 0) {
// I'm probably? likely? invalid.
});
where SelectImagePlaceholder is an Image Control.. =)
StorageFile file;
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
try
{
// Set the image source to the selected bitmap
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
SelectImagePlaceholder.Source = bitmapImage;
//SelectImagePlaceholder.HorizontalAlignment = HorizontalAlignment.Center;
//SelectImagePlaceholder.Stretch = Stretch.None;
this.SelectImagePlaceholder.DataContext = file;
_curMedia = file;
}
catch (Exception ex)
{
//code Handle the corrupted or invalid image
}
}