trevor1940
asked on
C#:WinForms Overlay a picturebox image with a gif
Hi
I have a windows form application that gathers info about my Movies using the TMDB API
the form shows info about people with the movies they are in How might I overlay the movie poster with a tick box (see Picture for mock up of what I want to do) if it is in the database?
The Poster image is from the internet https://image.tmdb.org/t/p/w92/2QoLPgwWphEO2eVDm6ehGxCWoXV.jpg
and the green tick is a transparent gif or png
Each movie is loaded via a UserControl this is where I'd put the code with a bool InDB = true/ false
I have a windows form application that gathers info about my Movies using the TMDB API
the form shows info about people with the movies they are in How might I overlay the movie poster with a tick box (see Picture for mock up of what I want to do) if it is in the database?
The Poster image is from the internet https://image.tmdb.org/t/p/w92/2QoLPgwWphEO2eVDm6ehGxCWoXV.jpg
and the green tick is a transparent gif or png
Each movie is loaded via a UserControl this is where I'd put the code with a bool InDB = true/ false
using System;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoviesDataModel;
using TMDbLib.Client;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.TvShows;
using TMDbLib.Objects.Search;
using TMDbLib.Objects.People;
namespace FilmsDB
{
public partial class KnownForMovies : UserControl
{
TMDbClient client = new TMDbClient(ConfigurationManager.AppSettings.Get("API_Key"));
string ImgPath = ConfigurationManager.AppSettings.Get("ImgPath");
static string ImgURL = ConfigurationManager.AppSettings.Get("ImgURL");
string OriginalImgPath = ConfigurationManager.AppSettings.Get("OriginalImgPath");
string MediaFiles = ConfigurationManager.AppSettings.Get("MediaFiles");
public string ThumbPhotoPath { get; set; }
public string FullPhotoPath { get; set; }
//private MovieRole pKF;
public string MovieTitle { get; set; }
private PersonUC personUC;
public KnownForMovies(PersonUC personUC)
{
this.personUC = personUC;
InitializeComponent();
if (personUC.PosterPath == null)
{
ThumbPhotoPath = ImgPath + "JohnWayneSml.png";
}
else
{
// Overlay images here if InDB == True
ThumbPhotoPath = ImgURL + "w92" + personUC.PosterPath;
FullPhotoPath = ImgURL + "original" + personUC.PosterPath;
}
TitleLbl.Text = personUC.Title + " (" + personUC.Year + " )";
if (personUC.Charecter != null)
{
CharacterLbl.Text = personUC.Charecter;
}
else
{
CharacterLbl.Visible = false;
}
MoviePosterPictureBox.LoadAsync(ThumbPhotoPath);
}
}
}
ASKER
The Logic of placing 1 image over the other works
Are you using a transparent gif or png?
I've tried with several .png & gif but don't seem to be able to make them transparent
Are you using a transparent gif or png?
I've tried with several .png & gif but don't seem to be able to make them transparent
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MoviePosterPictureBox.LoadAsync("https://image.tmdb.org/t/p/w92/2QoLPgwWphEO2eVDm6ehGxCWoXV.jpg");
pictureBox1.BackColor = System.Drawing.Color.Transparent;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = !pictureBox1.Visible;
button1.Text = $"{(pictureBox1.Visible ? "Hide" : "Show")} Checkmark";
}
}
Yes, you want to ensure that you use a png with an transparent alpha channel background. Also notice that I set the BackColor of the overlayed PictureBox to Transparent.
-saige-
-saige-
ASKER
Ok this is frustrating!
To prove the image has a transparent background I knocked up a html/css page
Using the same image in WinForm I get the pic bellow (The second white square is an empty picture box) as you can see from the Resources.resx file both have "BackColor = System.Drawing.Color.Trans parent;"
Even if the png file isn't compatible with WinForms I would expect pictureBox2 not to display because no picture has been loaded
To prove the image has a transparent background I knocked up a html/css page
Using the same image in WinForm I get the pic bellow (The second white square is an empty picture box) as you can see from the Resources.resx file both have "BackColor = System.Drawing.Color.Trans
Even if the png file isn't compatible with WinForms I would expect pictureBox2 not to display because no picture has been loaded
namespace OverLayingPictureBox
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.MoviePosterPictureBox = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.MoviePosterPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(41, 195);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 3;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MoviePosterPictureBox
//
this.MoviePosterPictureBox.Location = new System.Drawing.Point(26, 12);
this.MoviePosterPictureBox.Name = "MoviePosterPictureBox";
this.MoviePosterPictureBox.Size = new System.Drawing.Size(92, 138);
this.MoviePosterPictureBox.TabIndex = 1;
this.MoviePosterPictureBox.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.Location = new System.Drawing.Point(85, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.Location = new System.Drawing.Point(41, 103);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(32, 32);
this.pictureBox2.TabIndex = 5;
this.pictureBox2.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.MoviePosterPictureBox);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.MoviePosterPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox MoviePosterPictureBox;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
As I stated, you will need to set the location of pictureBox1 so that it is within the boundaries of MoviePosterPictureBox;
MoviePosterPictureBox -
-saige-
MoviePosterPictureBox -
this.MoviePosterPictureBox.Size = new System.Drawing.Size(92, 138);
pictureBox1 -
this.pictureBox1.Location = new System.Drawing.Point(85, 12);
The size of pictureBox1 is 32, 32, so let's subtract 34 from 92 (width of the MoviePosterPictureBox) and the change pictureBox1.Location to -
this.pictureBox1.Location = new System.Drawing.Point(58, 3);
See if that doesn't work for you.-saige-
ASKER
Thanx Very much
Got There in the end
Got There in the end
Form1.cs -
Open in new window
Form1.Designer.cs -Open in new window
Produces the following output -And after we show a couple of the checkmarks --saige-