Tags

Here is the simple custom webpart which random displays the image from picture library.

You can edit the web part by change the URL and picture library name
Random Image Web Part

public class RandomImage : System.Web.UI.WebControls.WebParts.WebPart
    {
        string _SiteURL = SPContext.Current.Web.Url;
        string _PicLibName = "Pic Lib";

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("The Site URL of Picture Library")]
        public string SiteURL
        {
            get
            {
                return _SiteURL;
            }
            set
            {
                _SiteURL = value;
            }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("The Name of Picture Library")]
        public string PicLibName 
        {
            get
            {
                return _PicLibName;
            }
            set
            {
                _PicLibName = value;
            }
        }

        Image myimage = new Image();
        protected override void CreateChildControls()
        {
            myimage.Height = 256;
            myimage.Width = 256;
            try
            {
                using (SPSite mysite = new SPSite(SiteURL))
                {
                    //mysite = SPControl.GetContextSite(Context);

                    using (SPWeb myweb = mysite.OpenWeb())
                    {
                        SPList mylist = myweb.Lists[PicLibName];
                        SPQuery myquery = new SPQuery();
                        myquery.Query = "";

                        string serverpath = mysite.Url.ToString() + "/";
                        SPListItemCollection mylistitem = mylist.GetItems(myquery);
                        if (mylistitem.Count > 0)
                        {
                            System.Random RandNum = new System.Random();
                            int MyRandomNumber = RandNum.Next(mylistitem.Count);
                            myimage.ImageUrl = serverpath + mylistitem[MyRandomNumber].Url.ToString();
                        }
                        else
                        {
                            this.Page.Response.Write("No image found");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Controls.Clear();
                Label errorMessage = new Label();
                errorMessage.Text = "There was an error in the code.  Please contact your system administrator and rely the following " +
                    "message: " + ex.Message;
                Controls.Add(errorMessage);
            }

            base.CreateChildControls();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            myimage.RenderControl(writer);
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");
        }
    }