|
One issue is that you're hard-coding the width of the final image to 5000 wide rather than using the "width" variable where you've been keeping track of the actual desired width.
|
|
|
|
|
I've done that using the width and height from the foreach loop. But when I pass these vars I get and exception of Parameter is not valid.
|
|
|
|
|
You're getting that error as you don't have enough available memory to hold the image. When you create a bitmap using the constructor that only supplies the width and height;
new System.Drawing.Bitmap(width, height);
the bitmap defaults to a 32-bit colour bitmap so every pixel needs 32bits of memory. If you don't need that many colours you can specify your own colour depth which will reduce the amount of memory used per pixel so you can have bigger images.
finalImage = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
If that is ok for your image size you can play with the options in PixelFormat to see what the best colour depth you can achieve.
If you google "c# large bitmap" you'll find other discussions about this, if the above doesn't work you might need to do something far more advanced like generate the proper byte streams that constitute a valid image in your desired format, but that will probably require pretty advanced knowledge of jpegs. There might also be a custom library out there that someone has already written to handle large bitmaps.
|
|
|
|
|
Thank you I'll try that now.
|
|
|
|
|
The original pdf pages images are roughly 396kb.
|
|
|
|
|
PDF files aren't stored as bitmaps though. If you take 3k of html in a browser then take a screen shot of the browser it's going to be way bigger than 3k.
|
|
|
|
|
Thank you I appreciate your reply.
I'm not well versed in creation of bitmaps. Bare with me.
Even if your code works I don't think this answers my question. The problem is not creation of the bitmap.
I can create the images from the pdf pages fine. Say I have a pdf with 2 pages. I create two jpg images. I Then create a bigger jpg image to house the two images above. This also works just fine. My last step is to remove "delete" the unneeded two jpg images from above. When my code deletes the two jpg images the merged image no longer works.
|
|
|
|
|
What do you mean by the merged image no longer works? I tried your code and it worked ok. Step through it in the debugger and put a breakpoint on the .Save method of the final image, run that line and check the image is ok via the file explorer. Then step through the loop that deletes the files and see if there is any difference in the saved final image. I tried your code and it worked ok, I'm struggling to see why deleting files after you've saved your merged image would affect that saved image.
|
|
|
|
|
The merged image is where I draw (merge) the images created from the pdf pages.
So if I have a 5 page pdf I will end up with 5 jpgs. I then combine these 5 jpgs onto one big canvas basically another jpg image that houses the 5 images. The original 5 jpgs are then deleted from the file system so that I now end up with just one jpg.
Seems to all be working except when I delete the 5 jpgs the canvas will only show the first page.
|
|
|
|
|
BTW thank you for replying.
|
|
|
|
|
I dynamically create 17 checkbox controls to one div which is set to runat="server" and than I am hiding that div, create the same 17 checkbox's to retrieve the checked value from them and create 30+ dynamically created additional checkbox's in another div.
When I check the first checkbox I created(in the third round of dynamically created checkboxes) and using a method to iterate through them/the second div to check if the first one is indeed checked to true I find out that it is not, ONLY when I get to the 18th checkbox, that I did not checked it is marked checked.
For some reason the creation of the first checkboxes in the second round after postback to the server affects the third round of checkboxes I create, why does it happens? And how to fix it?
|
|
|
|
|
You've got a secret error somewhere in your secret code. You should fix that.
Seriously, how do you think anyone can help you fix your code without seeing the relevant parts of your code?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Because I don't think its connected to my code, its something about dynamic controls or viewstate that I don't understand, and therefore can't answer myself..(I think)
but anyway... here is the code of the page
<pre> <%----%>
<div id ="registrationP3" runat="server" visible="false">
<div id="CheckboxProf" class="radios" runat="server" visible="false" Enableviewstate="false">
</div>
</div>
<%----%>
<div id ="registrationP4" runat="server" visible="false">
<div id="CheckboxProg" class="radios" runat="server" visible="false" EnableViewState="false">
</div>
<asp:Button ID="register" runat="server" Text="Register" OnClick="register_Click" />
</div>
<asp:Button ID="next" runat="server" Text="next" OnClick="next_Click" />
here is the code that creates the checkboxes:
<pre>public static void BindProfessions(HtmlControl ctrl, Page thispage)
{
List<Profession> Plist = Profession.GetProfessionList();
foreach (Profession p in Plist)
{
HtmlInputCheckBox rd_button = new HtmlInputCheckBox();
const string GROUP_NAME = "Professions";
rd_button.Name = GROUP_NAME;
string LinkID = "P" + p.ProfessionID.ToString();
rd_button.Attributes["id"] = LinkID;
rd_button.Value = p.ProfessionID.ToString();
RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
userprofession.imgP = p.ProfPath;
userprofession.fieldName = p.ProfName;
userprofession.IDnum = p.ProfessionID;
userprofession.RadioName = LinkID;
userprofession.EnableViewState = false;
rd_button.EnableViewState = false;
ctrl.Controls.Add(rd_button);
ctrl.Controls.Add(userprofession);
}
}
public static void BindKnowledge(HtmlControl ctrl, Page thispage)
{
List<Knowledge> Plist = Knowledge.RetKnowledgeList();
foreach (Knowledge p in Plist)
{
HtmlInputCheckBox checkBox = new HtmlInputCheckBox();
const string GROUP_NAME = "knowledge";
checkBox.Name = GROUP_NAME;
string LinkID = "Know" + p.ProgramID.ToString();
checkBox.Attributes["id"] = LinkID;
checkBox.Value = p.ProgramID.ToString();
RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
userprofession.imgP = p.ProgPath;
userprofession.fieldName = p.PName;
userprofession.IDnum = p.ProgramID;
userprofession.RadioName = LinkID;
userprofession.EnableViewState = false;
checkBox.EnableViewState = false;
ctrl.Controls.Add(checkBox);
ctrl.Controls.Add(userprofession);
}
}
here is the code that checkes the checkboxes after the user clicks submit
<pre> public static List<int> GetCheckBox(HtmlControl ctrl)
{
List<int> id_list = new List<int>();
foreach (Control rdButton in ctrl.Controls)
{
if (rdButton is HtmlInputCheckBox)
{
HtmlInputCheckBox bu = (HtmlInputCheckBox)rdButton;
if (bu.Checked)
{
id_list.Add(int.Parse(bu.Value));
}
}
}
return id_list;
}
here is the Page_Load in which I create the checkboxes once again with BindProfession, pay attention to the third if
protected void Page_Load(object sender, EventArgs e)
{
IsPageRefresh = false;
if (!IsPostBack)
{
ViewState["DivID"] = 1;
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
if (int.Parse(ViewState["DivID"].ToString()) == 3)
{
BindProfessions(CheckboxProf, Page);
}
else if(int.Parse(ViewState["DivID"].ToString()) == 4)
{
BindKnowledge(CheckboxProg, Page);
}
}
|
|
|
|
|
Hi, I am trying to get some temperature measurement data that I have stored in a SQL data base over on a webpage through asp.net.
Instead of getting timestamp with temperature measurement numbers I am getting some code text.
Here is my C# code in asp.net
This is Measurement.cs file
Am working in Microsoft Visual studio:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Microsoft.JSInterop.Infrastructure;
using Microsoft.AspNetCore.Hosting.Server;
using System.Threading.Tasks;
namespace AirHeaterControlSystem.Models
{
public class Measurement
{
public string Timestamp { get; set;}
public string MeasurementValue { get; set; }
public string ControlValue { get; set; }
public List<Measurement> GetMeasurementParameters()
{
List<Measurement> measurmentParameterList = new List<Measurement>();
string connectionString = "DATA SOURCE=LAPTOP-1T8PALVT\\CITADEL;UID=LAPTOP-1T8PALVT\\AdisP;DATABASE=MEASUREMENTDATA;Integrated Security = True;" ;
SqlConnection con = new SqlConnection(connectionString);
string sqlQuery = "Select Timestamp,MeasurementValue,ControlValue from MEASUREMENTDATA";
con.Open();
SqlCommand cmd = new SqlCommand(sqlQuery, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
Measurement measurementParameter = new Measurement();
measurementParameter.Timestamp = dr["TimeStamp"].ToString();
measurementParameter.MeasurementValue = dr["MeasurementValue"].ToString();
measurementParameter.ControlValue = dr["ControlValue"].ToString();
measurmentParameterList.Add(measurementParameter);
}
}
return measurmentParameterList;
}
}
}
Here is my cshtml file,
@page
@model AirHeaterControlSystem.Pages.MeasurementModel
@{
ViewData["Title"] = "Measurement Parameters";
}
<div>
<h1> Airheater temperature measurement</h1>
<table class="table">
<thead>
<tr>
<th>Timestamp</th>
<th>MeasurementValue</th>
<th>ControlValue</th>
</tr>
</thead>
<tbody>
@foreach (var measurement in Model.measurmentParameterList)
{
<tr>
<td> "measurement.Timestamp</td>
<td> "measurement.MeasurementValue</td>
<td> "measurement.ControlValue</td>
</tr>
}
</tbody>
</table>
</div>
|
|
|
|
|
Well, your view is wrong to start with. You've put:
<td> "measurement.Timestamp</td> when it should be:
<td> @measurement.Timestamp</td>
You should also be wrapping the disposable objects in a using block:
public List<Measurement> GetMeasurementParameters()
{
const string connectionString = "DATA SOURCE=LAPTOP-1T8PALVT\\CITADEL;UID=LAPTOP-1T8PALVT\\AdisP;DATABASE=MEASUREMENTDATA;Integrated Security = True;" ;
const string sqlQuery = "Select Timestamp, MeasurementValue, ControlValue from MEASUREMENTDATA";
List<Measurement> measurmentParameterList = new List<Measurement>();
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
{
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Measurement measurementParameter = new Measurement();
measurementParameter.Timestamp = dr["TimeStamp"].ToString();
measurementParameter.MeasurementValue = dr["MeasurementValue"].ToString();
measurementParameter.ControlValue = dr["ControlValue"].ToString();
measurmentParameterList.Add(measurementParameter);
}
}
}
return measurmentParameterList;
}
Beyond that, we can't help you. You haven't told us what you mean by "some code text", and you haven't shown us any of the data from your database.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm sorry to bother you, I just have a question regarding how to validate a list of object when is inside other list... I do have a list of cities, each city has another list of zones, an example could be like this:
public class City
{
public int Id { get; set; }
public string CityName { get; set; }
public virtual List<Zone> Zones { get; set; } = new List<Zone>();
}
public class Zone
{
public int Id { get; set; }
public string ZoneName { get; set; }
public int CityId { get; set; }
public virtual City City { get; set; }
}
Now, as you can see, the City is the principal and the Zone is the dependent class. Now I decided to pass what I have into my database to a view with a form to make changes to these Cities and their Zones, so I created a view model like this:
public class SettingsModel
{
public City NewCity { get; set; }
public List<City> CurrentCities { get; set; }
}
The first property is to create a new City that is not in the db. The other list contains the Cities already in the database. So I started by creating a list of forms using a for loop (not foreach) that uses an index. At the end, it produces something like this:
<form method="post" action="/Administration/UpdateCity">
...
<input type="hidden" id="CurrentCities_0__Id" name="CurrentCities[0].Id" value="7">
<input type="text" id="CurrentCities_0__CityName" name="CurrentCities[0].CityName" value="Austin">
...
</form>
...
<form method="post" action="/Administration/UpdateCity">
...
<input type="hidden" id="CurrentCities_1__Id" name="CurrentCities[1].Id" value="4">
<input type="text" id="CurrentCities_1__CityName" name="CurrentCities[1].CityName" value="Dallas">
...
</form>
as far as I understand, in the receiving action method (POST), I should use the [Bind(Prefix="CurrentCities")] to remove the unnecessary class name of the field name and, should specify a list<city> (or IEnumerable or Array) to tell that the object received is an array. in other words:
public IActionResult UpdateCity([Bind(Prefix = "CurrentCities")]List<City> myCity)
{
...
}
My question is: When I try to add a form in the same way for the Zones property of City, I end with the form fields named as:
<input type="text" id="CurrentCities_0__Zones_0__ZoneName" name="CurrentCities[0].Zones[0].ZoneName" value="Austin Metro Area">
...
<input type="text" id="CurrentCities_1__Zones_0__ZoneName" name="CurrentCities[1].Zones[0].ZoneName" value="San Antonio Metro Area">
As you can see, now all the fields have an extra index indicator like this "CurrentCities[0].Zones[0]" and I was wondering, how can I get this object in the POST action method? how can I specify a Bind Prefix of this kind? and how could I specify that the item is a collection item of other collection item?? Please help and thank you for your patience!
|
|
|
|
|
You don't need to mess around with the binding prefix; just have your update method accept an instance of your view-model, and let the model binder do the rest.
public IActionResult UpdateCity(SettingsModel model)
{
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Has anyone come across this problem where firefox won't allow file uploads. On IE works fine.
Is there a workaround?
Thank you in advance.
|
|
|
|
|
No. There's almost certainly a problem with your code, which you haven't shown.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
My upload function code.
I did find a solution below you'll notice I force all upload files to be placed in the end user's C:\Temp folder. I also convert pdf files to an image before the encryption.
if (!String.IsNullOrEmpty(btnBrowse.FileName.ToString()))
{
if (fileExt == ".pdf")
{
pdfToJpg = @"c:\temp\" + btnBrowse.FileName.ToString().ToLower();
jpgOut = System.IO.Path.GetFileNameWithoutExtension(btnBrowse.FileName.ToString().ToLower());
jpgOut = @"c:\temp\" + jpgOut + ".jpg";
success = Utilities.Pdf2Jpg(pdfToJpg, jpgOut);
if (!success)
{
hdnFieldAlert.Value = "error; ERROR! File Not Converted.;Document Upload";
return;
}
imagePath = jpgOut;
savePath = appPath + saveDir + System.IO.Path.GetFileNameWithoutExtension(btnBrowse.FileName.ToString().ToLower()) + ".jpg";
}
sqlCmd = new SqlCommand("select legaldocurl from [dbo].[refugee] where [aregnum] = @clientNum", sqlCon);
sqlCmd.Parameters.Add("@clientNum", SqlDbType.VarChar).Value = oARegNum;
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read() && !String.IsNullOrEmpty(dr["legaldocurl"].ToString()))
{
prevUpload = dr["legaldocurl"].ToString();
prevUploadFile = appPath + saveDir + prevUpload;
if (File.Exists(prevUploadFile)) File.Delete(prevUploadFile);
if (File.Exists(savePath)) File.Delete(savePath);
}
string jpgFile = System.IO.Path.GetFileNameWithoutExtension(imagePath) + ".jpg";
fileToEncrypt = imagePath;
img = System.Drawing.Image.FromFile(fileToEncrypt);
Utilities.Encrypt(img, savePath);
img.Dispose();
sqlCon.Close();
sqlCmd = new SqlCommand("update [dbo].[refugee] set legaldocurl = @imgPath where [aregnum] = @clientNum", sqlCon);
sqlCmd.Parameters.Add("@imgPath", SqlDbType.VarChar).Value = jpgFile;
sqlCmd.Parameters.Add("@clientNum", SqlDbType.VarChar).Value = oARegNum;
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
refugeeSelectByRecordId();
|
|
|
|
|
You mentioned uploading the images, which suggests to me that this is a web application.
You cannot use the FileName property of the FileUpload control to access the file. It returns the path - or sometimes just the name - of the file on the client. That file will not be accessible on the server, where your code is running.
It might appear to work when you debug your code in Visual Studio. But that's only because, in that specific cast, the server and client are the same computer. Once you deploy to a real server, your code will stop working.
Either use the SaveAs method to save the uploaded file to the server, or use the InputStream property to read the content of the uploaded file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
due to security reasons I researched firefox does not return file location
so to circumvent this - upload files must be placed in the c:\temp folder.
Is there a better workaround?
|
|
|
|
|
You are doing it wrong. Read my previous message. You CANNOT access the client's file system from code running on the server!
Use the SaveAs method to save the uploaded file somewhere on your server. Or use the InputStream property to read the data from the uploaded file.
Trying to use the FileName property to read the file WILL NOT WORK!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
H Richard, I added the function code to my help request.
|
|
|
|
|
I meant to add this.
The problem with FireFox is not, not allowing uploads but rather it does not show the path of the uploaded file just the name. to circumvent this - upload files must be placed in the c:\temp folder
|
|
|
|