|
Mucking around with DataGridView, DataTable, DataView: this works, but, I suspect there's a better way to do this:
private void btnSave_Click(object sender, EventArgs e)
{
if (dt != null)
{
DataTable dtbl = dt.Clone();
if (dView != null)
{
foreach (DataRowView vrow in dView)
{
dtbl.ImportRow(vrow.Row);
}
}
dtbl.WriteXml(... filepath goes here .xml ... );
}
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
How about using the ToTable method on the view?
DataView.ToTable Method (System.Data) | Microsoft Docs[^]
if (dt != null)
{
DataTable dtbl = dView != null ? dView.ToTable() : dt.Clone();
dtbl.WriteXml(...);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Hallo, I'm trying to extract images from pdf.
The problem is with some images that, in pdf, are with the attribute Predictor. I think that, with this attribute, the resultant bytes are to be decoded with png algorith, but I don't find it.
Another problem is with images that are with 8 bit indexed, in the pdf file I don't file the palette, as in other pdf files. Is there someone who is expert with pdf images? I hope my question is understandable. Thank you.
|
|
|
|
|
Hi,
you seem to be peeking into the PDF file to extract what you need, and that is bound to give you surprises when several different PDF sources are involved.
There is another way to approach things, that may or may not meet your needs: using something like Ghostscript[^] you can get an image of a particular page of a PDF document (in any resolution you want), then operate on that.
The advantage is you don't need to deal with all the possible ways PDF stores/creates images; the disadvantages are (1) you only get the resolution you asked for and (2) if all you need is a picture you still have to locate it and extract it from the page image.
Hope this helps.
|
|
|
|
|
How to use ghostscript?i have installed it but i don't understand how to run it.
|
|
|
|
|
Ghostscript can be ran in a separate process; it takes parameters from its command line. It is one of the many programs that explain their parameters by running it with a /h, -h or -help argument in a command window.
Here is a method I once used, you would have to adapt it to your needs of course.
using System.Diagnostics;
using System.IO;
...
string toolsFolder=@"...";
...
private Bitmap getPngImageFromPDF(int resolution, int pageNumber, string pageName, string inName) {
string args=" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dTextAlphaBits=4 "+
"-r" + resolution + " -sPageList=" + pageNumber + " -sOutputFile=" + pageName + " " + inName;
string cmd=Path.Combine(toolsFolder, "gswin32c.exe");
ProcessStartInfo psi=new ProcessStartInfo(cmd, args);
psi.CreateNoWindow=true;
if (!withDebug) psi.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;
Process proc=Process.Start(psi);
proc.WaitForExit();
Bitmap bm=(Bitmap)Image.FromFile(pageName);
return bm;
}
Notes:
1. pageName is the name of the file that will be generated by ghostscript.
2. the generated file will be locked as long as the generated bitmap is alive.
3. you should Dispose() of the bitmap when you no longer need it.
|
|
|
|
|
I am not able to generate the png file, the file should have generated in the same folder of the pdf files?
|
|
|
|
|
If you have trouble with ghostscript, run it manually first, i.e. from inside a Command Prompt (you can copy/paste command lines into it) and look at what it tells you. Only when you're satisfied should you start using C# code and the Process class.
The easiest way to get the folder issues sorted is by putting the ghostscript exe and your C# exe in the same folder (say "Debug"); I expect ghostscript will then use paths relative to that ("Debug") folder.
NB: there is nothing to install about ghostscript, you can put the exe anywhere, as long as it can be found (e.g. because its folder is added to the PATH environment variable, or because it is in your "current directory"). Alternatively you can tell the Process class where it is, that is what my toolsFolder did.
Warning: if you specify a full or partial path for the output, you probably must make sure the folder exists before running ghostscript. And the output location must be writeable, so special folders such as C:\Program Files\ are a no-no.
I can't help you any further, it is all standard Windows behavior as far as I know.
|
|
|
|
|
In C#.Net. Drag and drop control groupBox1 while run time how to write code ?
|
|
|
|
|
|
I took a look at the link you sent for drag-and-drop controls, but I haven't found an example yet
|
|
|
|
|
The first link gives a complete walkthrough. Maybe you need to study it in closer detail.
|
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
AvalonEdit by icsharpcode[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I have the error shows an exception occur when I try ho Handel foreach function
var results = new List<result>();
foreach (var item in jsonData.items)
{
results.Add(new Result
{
Title = item.title,
Link = item.link,
Snippet = item.snippet,
});
}
And I got this error System.NullReferenceException: Object reference not set to an instance of an object.
please help
|
|
|
|
|
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values. And it's very unlikely for that code to be generating the exception, unless there is something very, very wrong with the data being returned.
So start with the debugger, and verify the exact line that generates the error. If it is that line, then you need to load the returned HTML into a string and have a close look at that. It may be as simple as there is nothing sensible returned because you aren't loading it into a browser that has a valid Agent string, or logged in user...
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Dear CodeProject members,
Hope everyone is well, Can someone please help me with a way to get Monitor information as i needed to include in the project i'm working to collect hardware information using C#.
I get the results for one Screen but i would like to get Manufacturer, Model, Serial Number and number of screens as our company have users that have more than 4 screens.
The WMImonitorid has this information and i don't know how to use in this case.
my references are
using System;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
private void WMI_Click(object sender, EventArgs e)
ManagementObjectSearcher objSearcher = null;
ManagementObjectCollection objCollection = null;
ManagementScope objScope = null;
ObjectQuery objQuery = null;
ConnectionOptions objOptions = null;
objOptions = new ConnectionOptions();
objOptions.Impersonation = ImpersonationLevel.Impersonate;
//options.Username = "Username";
//options.Password = "Password";
//options.Authority = "NTLMDOMAIN:GVA.ICRC.Priv";
objScope = new ManagementScope("\\\\Computer\\root\\cimv2:", objOptions);
objScope.Connect();
// Monitor connected to Computer
objQuery = new ObjectQuery("SELECT * FROM Win32_DesktopMonitor");
objSearcher = new ManagementObjectSearcher(objScope, objQuery);
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DesktopMonitor instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Description: {0}", queryObj["Description"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
Thank you and much appreciate your help.
|
|
|
|
|
Win32_DesktopMonitor was deprecated. It also will not tell you accurate information about the displays attached. You may not even get any information at all. On my machine right now, I've got a laptop built-in display and two external monitors. There's only a single instance of Win32_DesktopMonitor being returned and it just lists generic information. There's nothing on the manufacturer or model of the monitors, or anything else that would be useful.
About the only class that returns anything accurate is the Win32_VideoController class, but it won't return any monitor information.
About the only EASILY ACCESSIBLE place I've found that can get you information on the monitors is looking in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY. You're looking for the EDID data, which you're going to have to decode to get the vendor/model information.
Be warned. Everything under this key will give you the EDID data for EVERY monitor that has ever been attached to the system. It does NOT remove the data for monitors that are no longer attached!
The hard way to do it would be to call the Windows EnumDisplayDevices function in user32.dll.
|
|
|
|
|
Dear Dave as always your feedback is much appreciated.
Without bothering you would mind doing a small code based on that I could be a base to start.
Much appreciated and wish you a lovely evening
|
|
|
|
|
LOL. No matter which method you choose, none of them is a "small code".
This is going to be a research project. It's probably going to take you a few days.
|
|
|
|
|
I accept the challenge thank you for the help once more😂👍🙏
|
|
|
|
|
Maybe someone can help :) I can’t figure out how to make this code work for borderless Form
Form1
public partial class Form1 : Form
{
clsResize _form_resize;
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
_form_resize = new clsResize(this);
this.Load += _Load;
this.Resize += _Resize;
}
private void _Load(object sender, EventArgs e)
{
_form_resize._get_initial_size();
}
private void _Resize(object sender, EventArgs e)
{
_form_resize._resize();
}
private const int cGrip = 16;
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc = new Rectangle(ClientSize.Width - cGrip, ClientSize.Height - cGrip, cGrip, cGrip);
ControlPaint.DrawSizeGrip(e.Graphics, BackColor, rc);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
{
Point pos = new Point(m.LParam.ToInt32());
pos = PointToClient(pos);
if (pos.X >= ClientSize.Width - cGrip && pos.Y >= ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17;
return;
}
}
base.WndProc(ref m);
}
clsResize.cs
public class clsResize
{
List<System.Drawing.Rectangle> _arr_control_storage = new List<System.Drawing.Rectangle>();
private bool showRowHeader = false;
public clsResize(Form _form_)
{
form = _form_;
_formSize = _form_.ClientSize;
_fontsize = _form_.Font.Size;
}
private float _fontsize { get; set; }
private System.Drawing.SizeF _formSize {get;set; }
private Form form { get; set; }
public void _get_initial_size()
{
var _controls = _get_all_controls(form);
foreach (Control control in _controls)
{
_arr_control_storage.Add(control.Bounds);
}
}
public void _resize()
{
double _form_ratio_width = (double)form.ClientSize.Width /(double)_formSize.Width;
double _form_ratio_height = (double)form.ClientSize.Height / (double)_formSize.Height;
var _controls = _get_all_controls(form);
int _pos = -1;
foreach (Control control in _controls)
{
_pos += 1;
System.Drawing.Size _controlSize = new System.Drawing.Size((int)(_arr_control_storage[_pos].Width * _form_ratio_width),
(int)(_arr_control_storage[_pos].Height * _form_ratio_height));
System.Drawing.Point _controlposition = new System.Drawing.Point((int)
(_arr_control_storage[_pos].X * _form_ratio_width),(int) (_arr_control_storage[_pos].Y * _form_ratio_height));
control.Bounds = new System.Drawing.Rectangle(_controlposition, _controlSize);
control.Font = new System.Drawing.Font(form.Font.FontFamily,
(float)(((Convert.ToDouble(_fontsize) * _form_ratio_width) / 2) +
((Convert.ToDouble(_fontsize) * _form_ratio_height) / 2)));
}
}
private static IEnumerable<Control> _get_all_controls(Control c)
{
return c.Controls.Cast<Control>().SelectMany(item =>
_get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
control.Name != string.Empty);
}
It works fine when FormBorderStyle.Sizable, but if FormBorderStyle.None get "System ArgumentOutOfRangeException" in this place:
_pos += 1;
System.Drawing.Size _controlSize = new System.Drawing.Size((int)(_arr_control_storage[_pos].Width * _form_ratio_width),
(int)(_arr_control_storage[_pos].Height * _form_ratio_height));
|
|
|
|
|
Start by using the debugger: put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. You need to find out which argument is out of range, and what the valid range is - until you have that, you can't start looking at why it's out of range!
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|