Creating image in asp.net aspx.cs file - c#

I am trying to create an image in asp.net then i want to change the image url in the code behind fie to display different images with different conditions. when i run the program i get an empty box with the description of the image. What should i do? thank you.
here is my code in the code behind file:
if (major2.Text == "ECE")
{
Image1.ImageUrl ="~/App_Data/Electrical_component.jpg";
Image1.AlternateText = "ECE";
Image1.Width = 250 ;
Image1.Height = 250;
}
if (major2.Text == "ENG")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.ImageUrl = "~/App_Data/eng.jpg";
Image1.AlternateText = "english";
}
if (major2.Text == "PHY")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.AlternateText = "physics";
Image1.ImageUrl = "~/App_Data/PhysicsBanner.jpg";
}
if (major2.Text == "MEC")
{
Image1.Width = 250;
Image1.Height = 250;
Image1.AlternateText = "mechanical";
Image1.ImageUrl = "~/App_Data/mechanical-engineers_00062336.jpg";
}

Open up your webpage in Google Chrome, and open the F12 Developer Options (I think this is the third time today I'm offering this advice to someone !!)
Go into the Network tab, then try to trigger one of your images appearing.
Does the Network tab show your webpage is trying to load the appropriate image file ?
If so, does it produce a "404" not found error, or does it successfully load that image ?
It's also worth checking that your image files have been set to "Copy to output" in Visual Studio, to make sure they are included in your site. But if you hit a "404" error above, that'll point this problem out to you.
Finally, does your C# code (shown in the question) actually get run? If you put a breakpoint on these lines, does it get hit ? And does a Postback occur after setting the Image attributes, which might also explain the images disappearing again.
Good luck!

Related

c# Play a second video file using Vlc.DotNet.Forms

I am hoping someone can help me, i want to play another video file, however when i double click another video file it doesnt play it and just shows the last frame from the first video file. i am not sure what i am doing wrong.
below is my code
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string final = "file path";
playfile(final);
}
void playfile(string final)
{
var control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
control.ResetMedia();
control.Play(new Uri(final).AbsoluteUri);
}
i have tried things like
control.Dispose();
control.ResetMedia();
control.Update();
control.Refresh();
i have even tried to manipulate the panel however that didnt work
Thank you
seems as if i did not clear the panel correctly if anyone is having trouble with the same issue i used this
panel1.Controls.Clear();
panel1.Update();
panel1.Refresh();
You don't need to destroy/add a new control each time. You could use the same player and just use
control.Play(newMedia);

Print Options(Excel Iterop) Issue with horizontal page breaks?

I am having problems with setting horizontal page breaks. No matter what row I set the first page break to it ALWAYS is set the row 87
This is the code I am using. (I checked to see if I was accidentally setting the page break again below this code, but I was not) So this is all of the code I am using to setup the print settings.
int pageCount = 2;
string defprinter = null;
defprinter = xlApplication.ActivePrinter;
xlWorkSheet.ResetAllPageBreaks();
xlApplication.ActivePrinter = defprinter;
var with = xlWorkSheet.PageSetup;
with.PaperSize = Excel.XlPaperSize.xlPaperA4;
with.Orientation = Excel.XlPageOrientation.xlPortrait;
// Fit Sheet on One Page
with.Zoom = false;
with.FitToPagesWide = 1;
with.PrintArea = #"$A$1:$I$" + (pageCount * 71);
with.FitToPagesTall = pageCount;
xlWorkSheet.HPageBreaks.Add(xlWorkSheet.Range["A72"]);
xlWorkSheet.HPageBreaks.Add(xlWorkSheet.Range["A143"]);
// Normal Margins
with.LeftMargin = xlApplication.InchesToPoints(0.5);
with.RightMargin = xlApplication.InchesToPoints(0.5);
with.TopMargin = xlApplication.InchesToPoints(0.5);
with.BottomMargin = xlApplication.InchesToPoints(0.5);
I have spent the better part of two days trying all different settings with no luck. No matter what I do, the first PageBreak is always set the row 87 and not row 72.
Any suggestions would be greatly appreciated.
EDIT: Tried this code on a different PC today and it works as it should. So now the issue is identified as being something to do with what is going on in Excel on that particular machine. OS is Windows 10 BTW.
So even though it works, this is still an issue as I can't have it sporadically working on some users PC's and not on others.

How can I add an image to my sheet without being prompted to replace contents of the (empty) destination cells (EPPlus)?

When I open the Excel file I generate using EPPlus (C#), I get "Do you want to replace the contents of the destination cells in [... .xlsx]PivotTable?"
As you can see in the screen shot below, there is nothing in the cell where I want the image to display. Even when I push the image beyond the outer edge of the PivotTable, I get this message (I had been placing beneath column C/3). When I select yes to the dialog quoted above, the image is finally placed on the sheet (if I select "no" it is not added), and the sheet looks like this:
This is the code I'm using to add the image:
int imageCol = _grandTotalsColumnPivotTable + 1; // -1; <= I really want it to be this
AddImage(pivotTableWorksheet, 1, imageCol);
. . .
private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex)
{
var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", _logo);
excelImage.From.Column = colIndex - 1;
excelImage.From.Row = rowIndex - 1;
excelImage.SetSize(130, 100);
excelImage.From.ColumnOff = Pixel2MTU(2);
excelImage.From.RowOff = Pixel2MTU(2);
}
public int Pixel2MTU(int pixels)
{
int mtus = pixels * 9525;
return mtus;
}
How can I either get the message to not display (programmatically, this can't be something the user needs to do) and "just do it" or let it know that there is nothing in that location, anyway?
UPDATE
This is apparently not the problem; I tried to delete this post, but can't, since it has an answer. Even when I comment out the writing of the image, I get it. And trying to prevent messages like so:
_xlApp.DisplayAlerts = false;
...does not work, either - I still get it.
Apparently the problem is elsewhere, but I don't know where.
UPDATE 2
It was a case of hidden data underneath the PivotTable; once I got rid of that, the problem went away. IOW, the problem was with my code, not with EPPlus.
I use this code to add an image to Excel. It does not asks me to replace content. But I do not use PivotTables. Maybe that would make a difference.
using (System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("test.png")))
{
var excelImage = ws.Drawings.AddPicture("My Logo", image);
excelImage.SetPosition(1, 0, 5, 0);
}

c# how to clone picture box 1 into pb 2?

I use two functions to load live camera and picture into picture box 1 and I want the same to be displayed in picture box2 also...
whatever comes in pb1 should also come in pb2.
Unfortunately, PictureBox does not have event like OnImageChanged, so you got to work around with it.
One way to work around with it is by creating your own MyPictureBox class derived from PictureBox (winform) in which it has its own (overshadowing) Image property. Then in the class you declare ImageChanged event and well as its handler. Then, in the setter of the Image property, you could call ImageChanged event.
When the ImageChanged occurs, you can change the other PictureBox image too.
Alternatively, you may want to make use of the existing (similar) LoadCompleted event of the PictureBox and then triggers the other PictureBox to get the new image.
See if any method may work for you.
Hi #user3004860 in my sample , I have quietly insert picture to the file folder when I put a picture in the box , so that you can use it in anywhere ,hopefully my idea can help you to deal with your question , as following code is the sample
if (!string.IsNullOrEmpty(fileExt))
{
var strExt = fileExt.ToLower();
var newFileName = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmss"), Random.Next(0, 9999).ToString("D4"), strExt);
var newOFileName = "o" + newFileName;
string imgUrl = "/Files";
var directory = new System.IO.DirectoryInfo(Server.MapPath("~"+imgUrl));
if (!directory.Exists)
directory.Create();
try
{
upload.SaveAs(directory + "/" + newOFileName);
string res;
res =
string.Format(
"top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('{0}{1}').closest('.mce-window').find('.mce-primary').click();",
imgUrl + '/', newOFileName);
return Content(res);
}
catch
{
return Content("error");
}
}

Couldn't assign value in FileUpload tool

I grabbed the value from database and I am trying to assign those values in Edit Form. But the only problem is with the FileUpload. It don't take the value. Can anyone suggest me what I'm missing here
private void EditForDataByID(int TitleId)
{
ReadmoreController objFormController = new ReadmoreController();
ReadMoreInfo objInfo = objFormController.GetListObjectOfAllArticle(TitleId);
if (objInfo != null)
{
TextTitle.Text = objInfo.Title;
txtSummary.Text = objInfo.Summary;
TextDate.Text = objInfo.Date.ToString();
//FileUpload1.FileName=objInfo.Image; I even tried this but it doesn't work
FileUpload1 = objInfo.Image;
Session["TitleId"] = TitleId;
ListDiv.Visible = false;
form.Visible = true;
BindGrid();
}
}
For client security reason you can not assign value to FileUploadControl as it could cause the uploading of unwanted files from client machine. So let the use pick the file to upload.
If it is allowed then one can steel the important files from client machine like c:\PersonalPasswords
Edit Based on comments
If you need to ensure that user has selected an Image and does not need to change it then you can use a image control and assign image to it. Use the same image control to find if the image is selected or not.

Categories

Resources