This question already has answers here:
Validate image from file in C#
(14 answers)
Closed 5 years ago.
I'm trying not using try-catch to achieve this goal
here's my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Bitmap imagePattern = new Bitmap(#"test.jpg");
Console.Read();
}
}
}
But if test.jpg is damage then C# will show error, so my question: is there a function like IsValidImage() in C#?
THX!
I think you can check the file header.
public static ImageType CheckImageType(string path)
{
byte[] buf = new byte[2];
try
{
using (StreamReader sr = new StreamReader(path))
{
int i = sr.BaseStream.Read(buf, 0, buf.Length);
if (i != buf.Length)
{
return ImageType.None;
}
}
}
catch (Exception exc)
{
//Debug.Print(exc.ToString());
return ImageType.None;
}
return CheckImageType(buf);
}
public static ImageType CheckImageType(byte[] buf)
{
if (buf == null || buf.Length < 2)
{
return ImageType.None;
}
int key = (buf[1] << 8) + buf[0];
ImageType s;
if (_imageTag.TryGetValue(key, out s))
{
return s;
}
return ImageType.None;
}
public enum ImageType
{
None = 0,
BMP = 0x4D42,
JPG = 0xD8FF,
GIF = 0x4947,
PCX = 0x050A,
PNG = 0x5089,
PSD = 0x4238,
RAS = 0xA659,
SGI = 0xDA01,
TIFF = 0x4949
}
Related
All the methods I've found are somehow UI dependent. So, how to capture all displays without any dependency and third party?
You can use GetSystemMetrics function with SM_CXVIRTUALSCREEN and SM_CYVIRTUALSCREEN parameters for entire desktop. It will be handle all virtual displays.
using System;
using System.ComponentModel;//You can remove it if you will catch the below exception from another layer
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace Helpers {
public static class HelperScreenShot {
public static void TakeAndSaveByAllScreen(ImageFormat imageFormat, string fullFileName = null) {
var img = CopyFromScreen();
if(img == null) return;
if(string.IsNullOrWhiteSpace(fullFileName)) {
fullFileName = $"{Guid.NewGuid()}.{imageFormat.ToString().ToLower()}";
}
img.Save(fullFileName, imageFormat);
}
public static byte[] TakeAsBinary(ImageFormat imageFormat) {
using var image = CopyFromScreen();
if(image == null) return default;
using var ms = new MemoryStream();
image.Save(ms, imageFormat);
return ms.ToArray();
}
private static Bitmap CopyFromScreen() {
try {
var size = HelperDisplay.GetVirtualScreenSize();
var image = new Bitmap(size.Width, size.Height);
using var graphics = Graphics.FromImage(image);
graphics.CopyFromScreen(Point.Empty, Point.Empty, size);
return image;
}
catch(Win32Exception) {//When screen saver is active
return null;
}
}
private static class HelperDisplay {
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(MetricType metricType);
public static Size GetVirtualScreenSize() {
var width = GetSystemMetrics(MetricType.VirtualScreenWidth);
var height = GetSystemMetrics(MetricType.VirtualScreenHeight);
return new Size(width, height);
}
private enum MetricType {
VirtualScreenWidth = 78,
VirtualScreenHeight = 79
}
}
}
}
Usage
var dir = Path.Combine("images", "screenshot");
var imgFormat = ImageFormat.Png;
var fileName = $"{Guid.NewGuid()}.{imgFormat.ToString().ToLower()}";
fileName = Path.Combine(dir, fileName);
if(!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}
HelperScreenShot.TakeAndSaveByAllScreen(fileName, imgFormat);
//or
var binaryImg = HelperScreenShot.TakeAsBinary(imgFormat);
Further Reading:
Multiple Monitor System Metrics
winuser.h header
How do you use System.Drawing in .NET Core?
I want to creating circle buffer in c#.
i have Arduino board that sending a lot of data
when discard buffer in serial port command some of data will be lost.
at this situation I need circle buffer for my data to ricived them .
InputData = ComPort.ReadByte();
object firstByte = InputData;
if (ComPort.IsOpen==true)
{
s = Convert.ToString(Convert.ToChar(firstByte));
temp1 += s;
lock (firstByte) {
if (Convert.ToInt32( firstByte)== 13)
{
temp = temp1;
temp1 = "";
LstGetInfo.BeginInvoke(new Action(()=>
{
if (temp !=null)
{
LstGetInfo.Items.Add(temp);
if (LstGetInfo.Items.Count >= 100)
{
LstGetInfo.Items.Clear();
// ComPort.DiscardInBuffer();
//ComPort.DiscardOutBuffer();
}
FileStream fs = new FileStream(filename, FileMode.Append);
var data = System.Text.Encoding.UTF8.GetBytes(String.Format("{0} {1}", temp, DateTime.Now.ToString("hh mm ss")) +"\r\n");
fs.Write(data, 0, data.Length);
fs.Close();
}
}));
LstGetInfo.BeginInvoke(new Action(() =>
{
LstGetInfo.TopIndex = LstGetInfo.Items.Count - 1;
}));
}
}
any solution for this problem ?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication72
{
class Program
{
static string buffer = "";
static void Main(string[] args)
{
}
static void AddToBuffer(byte[] rxData)
{
buffer += Encoding.UTF8.GetString(rxData);
}
static string ReadLine()
{
int returnIndex = buffer.IndexOf("\n");
if (returnIndex >= 0)
{
string line = buffer.Substring(0, returnIndex);
buffer = buffer.Remove(0, returnIndex + 1);
return line;
}
else
{
return "";
}
}
}
}
I'm running a windows application(x86) platform in a windows server 2012(x64) and I'm using a SubSonic to get data from the SQLserver.
And I got this error from try catch Exception
at SubSonic.SqlQuery.ExecuteAsCollectionListType at
CIS.Server.Automailer.Core.ReportConfig.getReportConfig() at
CIS.Server.Automailer.Automailer.processDownload() at
CIS.Server.Automailer.Program.Main()
Here's my source code:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace CIS.Server.Automailer
{
static class Program
{
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Automailer mailer = new Automailer();
mailer.processDownload();
Application.Run();
}
catch(Exception ex)
{
Log(ex);
}
}
static void Log(Exception ex)
{
string stackTrace = ex.StackTrace;
File.WriteAllText("trace.txt", stackTrace); // path of file where stack trace will be stored.
}
}
}
Automailer.cs
public void processDownload()
{
var data = ReportConfig.getReportConfig();
var machineCenterIds = data[0].MachineCenterId;
var reportIds = data[0].ReportId;
var email = data[0].Email;
recipient = email;
string[] splitRepId = reportIds.Split(',');
int[] repIds = new int[splitRepId.Length];
int c=0;
foreach (string repId in splitRepId)
{
repIds[c] = Convert.ToInt32(repId);
c++;
}
var reportNames = ReportConfig.getReportName(repIds);
string[] splitMcIds = machineCenterIds.Split(',');
int ctr = 0;
filePaths = new string[splitMcIds.Length * reportNames.Count()];
ei.processFinished = false;
foreach (string mcId in splitMcIds)
{
for (int i = 0; i < reportNames.Count(); i++)
{
string reportName = Convert.ToString(reportNames[i]);
string url = Utility.GetTemporaryURL(mcId, reportName);
string fileName = reportName.Replace(" ", "");// + "_" + j + "_00" + i
downloadPath = string.Format(configPath, mcId, fileName);
filePaths[ctr] = downloadPath;
GenerateMails(url);
Console.WriteLine(downloadPath);
ctr++;
}
}
processEmail();
ei.processFinished = true;
}
ReportConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CIS.Server.Automailer.Core
{
class ReportConfig
{
public static ReportAutomationCollection getReportConfig()
{
return DB.Select().From(ReportAutomation.Schema)
.Where(ReportAutomation.Columns.UserId).IsEqualTo("001111d6-cc2a-469a-a1bc-1ccd64e60a08")
.ExecuteAsCollection<ReportAutomationCollection>();
}
public static ReportTypeCollection getReportName(int[] reportId)
{
return DB.Select(ReportType.Columns.ReportName).From(ReportType.Schema)
.Where(ReportType.ReportIdColumn).In(reportId.ToString())
.ExecuteAsCollection<ReportTypeCollection>();
}
}
}
Seems like you have a problem with your database. since your access now is on the server.
check your data if both exist on your local and on the Server.
Just curious if there is a more simplified version to check if the given body has the word style of "Heading3" applied given this sample C# code I wrote learning the OpenXML library. To be clear, I am just asking given a body element how can I determine if the given body element has what word style applied. I eventually have to write a program that process numerous .DOCX files and need to process them from a top to bottom approach.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
namespace docxparsing
{
class Program
{
static void Main()
{
string file_to_parse = #"C:\temp\sample.docx";
WordprocessingDocument doc = WordprocessingDocument.Open(file_to_parse,false);
Body body = doc.MainDocumentPart.Document.Body;
string fooStr
foreach( var foo in body )
{
fooStr = foo.InnerXml;
/*
these 2 comments represent 2 different xml snippets from 'fooStr'. the only way i figure out how to get the word style is by reading
this xml and doing checks for values. i don't know of any other approach in using the body element to check for the applied word style
<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pStyle w:val="Heading2" />
<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pStyle w:val="Heading3" />
*/
bool hasHeading3 = fooStr.Contains("pStyle w:val=\"Heading3\"");
if ( hasHeading3 )
{
Console.WriteLine("heading3 found");
}
}
doc.Close();
}
}
}
// -------------------------------------------------------------------------------
EDIT
Here is updated code of one way to do this. Still not overall happy with it but it works. Function to look at is getWordStyleValue(string x)
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace docxparsing
{
class Program
{
// ************************************************
// grab the word style value
// ************************************************
static string getWordStyleValue(string x)
{
int p = 0;
p = x.IndexOf("w:pStyle w:val=");
if ( p == -1 )
{
return "";
}
p = p + 15;
StringBuilder sb = new StringBuilder();
while (true)
{
p++;
char c = x[p];
if (c != '"')
{
sb.Append(c);
}
else
{
break;
}
}
string s = sb.ToString();
return s;
}
// ************************************************
// Main
// ************************************************
static void Main(string[] args)
{
string theFile = #"C:\temp\sample.docx";
WordprocessingDocument doc = WordprocessingDocument.Open(theFile,false);
string body_table = "DocumentFormat.OpenXml.Wordprocessing.Table";
string body_paragraph = "DocumentFormat.OpenXml.Wordprocessing.Paragraph";
Body body = doc.MainDocumentPart.Document.Body;
StreamWriter sw1 = new StreamWriter("paragraphs.log");
foreach (var b in body)
{
string body_type = b.ToString();
if (body_type == body_paragraph)
{
string str = getWordStyleValue(b.InnerXml);
if (str == "" || str == "HeadingNon-TOC" || str == "TOC1" || str == "TOC2" || str == "TableofFigures" || str == "AcronymList" )
{
continue;
}
sw1.WriteLine(str + "," + b.InnerText);
}
if ( body_type == body_table )
{
// sw1.WriteLine("Table:\n{0}",b.InnerText);
}
}
doc.Close();
sw1.Close();
}
}
}
Yes. You could do something like this:
bool ContainsHeading3 = body.Descendants<ParagraphSytleId>().Any(psId => psId.Val == "Heading3");
This will look at all the ParagraphStyleId elements (w:pStyle in the xml) and see if any of them have the Val of Heading3.
Just pasting this Edit from original post so he has better visibility.
Here is one solution I came up with. Yes, it a little cody ( if that is a word ) but working LINQ ( my fav ) to optimize a more elegant solution.
--
Here is updated code of one way to do this. Still not overall happy with it but it works. Function to look at is getWordStyleValue(string x)
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace docxparsing
{
class Program
{
// ************************************************
// grab the word style value
// ************************************************
static string getWordStyleValue(string x)
{
int p = 0;
p = x.IndexOf("w:pStyle w:val=");
if ( p == -1 )
{
return "";
}
p = p + 15;
StringBuilder sb = new StringBuilder();
while (true)
{
p++;
char c = x[p];
if (c != '"')
{
sb.Append(c);
}
else
{
break;
}
}
string s = sb.ToString();
return s;
}
// ************************************************
// Main
// ************************************************
static void Main(string[] args)
{
string theFile = #"C:\temp\sample.docx";
WordprocessingDocument doc = WordprocessingDocument.Open(theFile,false);
string body_table = "DocumentFormat.OpenXml.Wordprocessing.Table";
string body_paragraph = "DocumentFormat.OpenXml.Wordprocessing.Paragraph";
Body body = doc.MainDocumentPart.Document.Body;
StreamWriter sw1 = new StreamWriter("paragraphs.log");
foreach (var b in body)
{
string body_type = b.ToString();
if (body_type == body_paragraph)
{
string str = getWordStyleValue(b.InnerXml);
if (str == "" || str == "HeadingNon-TOC" || str == "TOC1" || str == "TOC2" || str == "TableofFigures" || str == "AcronymList" )
{
continue;
}
sw1.WriteLine(str + "," + b.InnerText);
}
if ( body_type == body_table )
{
// sw1.WriteLine("Table:\n{0}",b.InnerText);
}
}
doc.Close();
sw1.Close();
}
}
}
I'm trying to implement a handler in my WPF application for files dragged from a zip archive. The handler should get the file content for further processing.
My environment: Windows7, 7-zip installed, Visual Studio 2012 Express, .Net 4.5
Here is the code of a simple MainWindow app to demonstrate the problem:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AllowDrop= true;
Drop += onDrop;
}
private void onDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("FileContents"))
{
var fileContents = e.Data.GetData("FileContents");
//get file contents...
}
}
}
When I drag a file contained in a zip archive to my Window the call to e.Data.GetData("FileContents") throws a System.ArgumentException ("Argument out of Range") with the following callstack:
System.Windows.DataObject.OleConverter.GetDataInner(formatetc, medium)
System.Windows.DataObject.OleConverter.GetDataFromOleHGLOBAL(format, aspect, index)
System.Windows.DataObject.OleConverter.GetDataFromBoundOleDataObject(format, aspect, index)
System.Windows.DataObject.OleConverter.GetData(format, autoConvert, aspect, index)
System.Windows.DataObject.OleConverter.GetData(format, autoConvert)
System.Windows.DataObject.GetData(format, autoConvert)
System.Windows.DataObject.GetData(format)
TestZip.MainWindow.onDrop(sender, e) Zeile 34 C#
I've looked up the source code of this OleConverter (http://reflector.webtropy.com/default.aspx/Dotnetfx_Win7_3#5#1/Dotnetfx_Win7_3#5#1/3#5#1/DEVDIV/depot/DevDiv/releases/Orcas/NetFXw7/wpf/src/Core/CSharp/System/Windows/dataobject#cs/1/dataobject#cs) but the GetDataInner() method is implemented like
private void GetDataInner(ref FORMATETC formatetc, out STGMEDIUM medium)
{
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); // BlessedAssert
try
{
_innerData.GetData(ref formatetc, out medium);
}
finally
{
SecurityPermission.RevertAssert();
}
}
So this does also not provide further info of what's wrong here.
I also tried with uninstalled 7-zip and with different zip archives, but no change.
My question: Does any one have a clue what's going wrong here? What do I need to do in order to get the content of a file from a zip-archive dropped onto my window?
Old question but I needed to do this today so....
Using statements:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
Reference PresentationCore
https://msdn.microsoft.com/en-us/library/system.windows.idataobject(v=vs.110).aspx
First you need to get the "FileDescriptor" contents, here's a class which reads them.
/// <summary>
/// Specifies which fields are valid in a FileDescriptor Structure
/// </summary>
[Flags]
enum FileDescriptorFlags : uint
{
ClsId = 0x00000001,
SizePoint = 0x00000002,
Attributes = 0x00000004,
CreateTime = 0x00000008,
AccessTime = 0x00000010,
WritesTime = 0x00000020,
FileSize = 0x00000040,
ProgressUI = 0x00004000,
LinkUI = 0x00008000,
Unicode = 0x80000000,
}
internal static class FileDescriptorReader
{
internal sealed class FileDescriptor
{
public FileDescriptorFlags Flags{get;set;}
public Guid ClassId{get;set;}
public Size Size{get;set;}
public Point Point{get;set;}
public FileAttributes FileAttributes{get;set;}
public DateTime CreationTime{get;set;}
public DateTime LastAccessTime{get;set;}
public DateTime LastWriteTime{get;set;}
public Int64 FileSize{get;set;}
public string FileName{get;set;}
public FileDescriptor(BinaryReader reader)
{
//Flags
Flags = (FileDescriptorFlags)reader.ReadUInt32();
//ClassID
ClassId = new Guid(reader.ReadBytes(16));
//Size
Size = new Size(reader.ReadInt32(), reader.ReadInt32());
//Point
Point = new Point(reader.ReadInt32(), reader.ReadInt32());
//FileAttributes
FileAttributes = (FileAttributes)reader.ReadUInt32();
//CreationTime
CreationTime = new DateTime(1601,1,1).AddTicks(reader.ReadInt64());
//LastAccessTime
LastAccessTime = new DateTime(1601,1,1).AddTicks(reader.ReadInt64());
//LastWriteTime
LastWriteTime = new DateTime(1601, 1, 1).AddTicks(reader.ReadInt64());
//FileSize
FileSize = reader.ReadInt64();
//FileName
byte[] nameBytes = reader.ReadBytes(520);
int i = 0;
while(i < nameBytes.Length)
{
if (nameBytes[i] == 0 && nameBytes[i + 1] == 0)
break;
i++;
i++;
}
FileName = UnicodeEncoding.Unicode.GetString(nameBytes, 0, i);
}
}
public static IEnumerable<FileDescriptor> Read(Stream fileDescriptorStream)
{
BinaryReader reader = new BinaryReader(fileDescriptorStream);
var count = reader.ReadUInt32();
while (count > 0)
{
FileDescriptor descriptor = new FileDescriptor(reader);
yield return descriptor;
count--;
}
}
public static IEnumerable<string> ReadFileNames(Stream fileDescriptorStream)
{
BinaryReader reader = new BinaryReader(fileDescriptorStream);
var count = reader.ReadUInt32();
while(count > 0)
{
FileDescriptor descriptor = new FileDescriptor(reader);
yield return descriptor.FileName;
count--;
}
}
}
Now using that you can get the matching file content for each file:
static class ClipboardHelper
{
internal static MemoryStream GetFileContents(System.Windows.IDataObject dataObject, int index)
{
//cast the default IDataObject to a com IDataObject
IDataObject comDataObject;
comDataObject = (IDataObject)dataObject;
System.Windows.DataFormat Format = System.Windows.DataFormats.GetDataFormat("FileContents");
if (Format == null)
return null;
FORMATETC formatetc = new FORMATETC();
formatetc.cfFormat = (short)Format.Id;
formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
formatetc.lindex = index;
formatetc.tymed = TYMED.TYMED_ISTREAM | TYMED.TYMED_HGLOBAL;
//create STGMEDIUM to output request results into
STGMEDIUM medium = new STGMEDIUM();
//using the com IDataObject interface get the data using the defined FORMATETC
comDataObject.GetData(ref formatetc, out medium);
switch (medium.tymed)
{
case TYMED.TYMED_ISTREAM: return GetIStream(medium);
default: throw new NotSupportedException();
}
}
private static MemoryStream GetIStream(STGMEDIUM medium)
{
//marshal the returned pointer to a IStream object
IStream iStream = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
Marshal.Release(medium.unionmember);
//get the STATSTG of the IStream to determine how many bytes are in it
var iStreamStat = new System.Runtime.InteropServices.ComTypes.STATSTG();
iStream.Stat(out iStreamStat, 0);
int iStreamSize = (int)iStreamStat.cbSize;
//read the data from the IStream into a managed byte array
byte[] iStreamContent = new byte[iStreamSize];
iStream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero);
//wrapped the managed byte array into a memory stream
return new MemoryStream(iStreamContent);
}
}
Now you can enumerate the streams in the file contents:
var fileDescriptor = (MemoryStream)Clipboard.GetDataObject().GetData("FileGroupDescriptorW");
var files = FileDescriptorReader.Read(fileDescriptor);
var fileIndex = 0;
foreach (var fileContentFile in files)
{
if ((fileContentFile.FileAttributes & FileAttributes.Directory) != 0)
{
//Do something with directories?
//Note that directories do not have FileContents
//And will throw if we try to read them
}
else
{
var fileData = ClipboardHelper.GetFileContents(Clipboard.GetDataObject(), FileIndex);
fileData.Position = 0;
//Do something with the fileContent Stream
}
fileIndex++;
}