SLIMDX antialising - antialiasing

I try to get the high qulity antialiasing from a tuturial I found on the internet (http://www.rkoenig.eu/index.php?option=com_content&view=article&id=21:chapter-3-das-erste-echte-3d-objekt&catid=6:directx10-basics&Itemid=3). But did not achieve a very good solution.
I already set the multisampling to the maximum:
m_swapChainDesc.SampleDescription = new DXGI.SampleDescription(8,0);
To me it appears as the pixel size of the rendered image is larger than the actual pixel size of my screen.
Thank you very much in advance for your valuable inputs
here is the complete code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SlimDX;
using DX10 = SlimDX.Direct3D10;
using DXGI = SlimDX.DXGI;
namespace TutorialSeries.DirectX10.Chapter3
{
public partial class MainWindow : Form
{
private DX10.Device m_device;
private DXGI.SwapChainDescription m_swapChainDesc;
private DXGI.SwapChain m_swapChain;
private DXGI.Factory m_factory;
private DX10.RenderTargetView m_renderTarget;
private bool m_initialized;
private SimpleBox m_simpleBox;
private Matrix m_viewMatrix;
private Matrix m_projMatrix;
private Matrix m_worldMatrix;
private Matrix m_viewProjMatrix;
public MainWindow()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.Opaque, true);
}
/// <summary>
/// Initializes device and other resources needed for rendering. Returns true, if successful.
/// </summary>
private bool Initialize3D()
{
try
{
m_device = new DX10.Device(DX10.DriverType.Warp, DX10.DeviceCreationFlags.SingleThreaded);
m_factory = new DXGI.Factory();
m_swapChainDesc = new DXGI.SwapChainDescription();
m_swapChainDesc.OutputHandle = this.Handle;
m_swapChainDesc.IsWindowed = true;
m_swapChainDesc.BufferCount = 1;
m_swapChainDesc.Flags = DXGI.SwapChainFlags.AllowModeSwitch;
m_swapChainDesc.ModeDescription = new DXGI.ModeDescription(
this.Width,
this.Height,
new Rational(60, 1),
DXGI.Format.R8G8B8A8_UNorm);
m_swapChainDesc.SampleDescription = new DXGI.SampleDescription(8,0);
m_swapChainDesc.SwapEffect = DXGI.SwapEffect.Discard;
m_swapChainDesc.Usage = DXGI.Usage.RenderTargetOutput;
m_swapChain = new DXGI.SwapChain(m_factory, m_device, m_swapChainDesc);
DX10.Viewport viewPort = new DX10.Viewport();
viewPort.X = 0;
viewPort.Y = 0;
viewPort.Width = this.Width;
viewPort.Height = this.Height;
viewPort.MinZ = 0f;
viewPort.MaxZ = 1f;
//DX10.Texture2D backBuffer = m_swapChain.GetBuffer<DX10.Texture2D>(0);
DX10.Texture2D Texture = DX10.Texture2D.FromSwapChain<DX10.Texture2D>(m_swapChain,0);
//m_renderTarget = new DX10.RenderTargetView(m_device, backBuffer);
//DX10.RenderTargetViewDescription renderDesc = new DX10.RenderTargetViewDescription();
//renderDesc.FirstArraySlice = 0;
//renderDesc.MipSlice = 0;
m_renderTarget = new DX10.RenderTargetView(m_device, Texture);
Texture.Dispose();
DX10.RasterizerStateDescription rsd = new DX10.RasterizerStateDescription();
rsd.CullMode = DX10.CullMode.Back;
rsd.FillMode = DX10.FillMode.Wireframe;
rsd.IsMultisampleEnabled = true;
rsd.IsAntialiasedLineEnabled = false;
rsd.IsDepthClipEnabled = false;
rsd.IsScissorEnabled = false;
DX10.RasterizerState RasterStateWireFrame = DX10.RasterizerState.FromDescription(m_device,rsd);
DX10.BlendStateDescription blendDesc = new DX10.BlendStateDescription();
blendDesc.BlendOperation = DX10.BlendOperation.Add;
blendDesc.AlphaBlendOperation = DX10.BlendOperation.Add;
blendDesc.SourceAlphaBlend = DX10.BlendOption.Zero;
blendDesc.DestinationAlphaBlend = DX10.BlendOption.Zero;
blendDesc.SourceBlend = DX10.BlendOption.SourceColor;
blendDesc.DestinationBlend = DX10.BlendOption.Zero;
blendDesc.IsAlphaToCoverageEnabled = false;
blendDesc.SetWriteMask(0, DX10.ColorWriteMaskFlags.All);
blendDesc.SetBlendEnable(0, true);
DX10.BlendState m_blendState = DX10.BlendState.FromDescription(m_device, blendDesc);
m_device.Rasterizer.State = RasterStateWireFrame;
m_device.Rasterizer.SetViewports(viewPort);
m_device.OutputMerger.BlendState = m_blendState;
m_device.OutputMerger.SetTargets(m_renderTarget);
m_viewMatrix = Matrix.LookAtLH(
new Vector3(0f, 0f, -4f),
new Vector3(0f, 0f, 1f),
new Vector3(0f, 1f, 0f));
m_projMatrix = Matrix.PerspectiveFovLH(
(float)Math.PI * 0.5f,
this.Width / (float)this.Height,
0.1f, 100f);
m_viewProjMatrix = m_viewMatrix * m_projMatrix;
m_worldMatrix = Matrix.RotationYawPitchRoll(0.85f, 0.85f, 0f);
m_simpleBox = new SimpleBox();
m_simpleBox.LoadResources(m_device);
m_initialized = true;
}
catch (Exception ex)
{
MessageBox.Show("Error while initializing Direct3D10: \n" + ex.Message);
m_initialized = false;
}
return m_initialized;
}
/// <summary>
/// Rendering is done during the standard OnPaint event
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_initialized)
{
m_device.ClearRenderTargetView(m_renderTarget, new Color4(Color.CornflowerBlue));
m_simpleBox.Render(m_device, m_worldMatrix, m_viewProjMatrix);
m_swapChain.Present(0, DXGI.PresentFlags.None);
}
}
/// <summary>
/// Initialize 3D-Graphics within OnLoad event
/// </summary>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Initialize3D();
}
}
}

This is an old question but it's a shame it never got answered; I stumbled across it on Google so I figure answering it may help someone else in the future...
First of all, Pascal, you did NOT set MSAA to the maximum... You're using 8:0, which means 8 samples at a quality of 0 (zero)... definitely not the maximum. What the "max" is depends on the GPU installed on the local machine. So it varies from PC to PC. That's why a DirectX application needs to use DXGI to properly enumerate hardware devices and determine what settings are valid. This is no trivial topic and will require you to do some research and practice of your own. The DirectX SDK Documentation and samples/tutorials are a great starting place, and there's a lot of other materials to be found online. But on my machine, for instance, my GTX-580 GPU can support 8:16 MSAA (possibly higher, but haven't checked).
So you need to learn to use DXGI to enumerate your graphics cards and monitors and figure out what MSAA levels (and other graphics features/settings) it can support. That's the only way you'll ever figure out the "max" MSAA settings or the correct refresh rate of your monitor, for example. If you're clever you will write yourself a small library or component for your game engine that will enumerate hardware devices for you and figure out the optimal graphics settings so you won't have to re-do this over and over for future projects.
Regards,
--ATC--

Related

Using UWP monitor live audio and detect gun-fire/clap sound

I am developing a new UWP app which should monitor sound and fire a event for each sudden sound blow (something like gun fire or clap).
It needs to enable default Audio Input and monitor live audio.
Set audio sensitivity for identifying environment noise and recognizing clap/gun-fire
When there is a high frequency sound like a clap/gun-fire sound (Ideally it should be like configured frequency like +/-40 then it is a gun-fire/clap) then it should call a event.
No need to save Audio
I tried to implement this
SoundMonitoringPage:
public sealed partial class MyPage : Page
{
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
string deviceId = Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId(Windows.Media.Devices.AudioDeviceRole.Communications);
gameChatAudioStateMonitor = AudioStateMonitor.CreateForCaptureMonitoringWithCategoryAndDeviceId(MediaCategory.GameChat, deviceId);
gameChatAudioStateMonitor.SoundLevelChanged += GameChatSoundLevelChanged;
//other logic
}
}
Sound Level Change:
private void GameChatSoundLevelChanged(AudioStateMonitor sender, object args)
{
switch (sender.SoundLevel)
{
case SoundLevel.Full:
LevelChangeEvent();
break;
case SoundLevel.Muted:
LevelChangeEvent();
break;
case SoundLevel.Low:
// Audio capture should never be "ducked", only muted or full volume.
Debug.WriteLine("Unexpected audio state change.");
break;
}
}
ENV: windows 10 (v1809) IDE: VS 2017
Not sure if this is the right approach. This is not enabling audio and not hitting the level change event.
I see other options in WinForms & NAudio tutorial here. Probably with Sampling frequency I can check events... Doesn't have must tutorial on using NAudio with UWP to plot the graph and identify the frequency.
Update:
Followed suggestion from #Rob Caplan - MSFT, here is what I ended up with
IMemoryBufferByteAccess.cs
// We are initializing a COM interface for use within the namespace
// This interface allows access to memory at the byte level which we need to populate audio data that is generated
[ComImport]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
void GetBuffer(out byte* buffer, out uint capacity);
}
GunFireMonitorPage.xaml.cs
public sealed partial class GunFireMonitorPage : Page
{
private MainPage _rootPage;
public static GunFireMonitorPage Current;
private AudioGraph _graph;
private AudioDeviceOutputNode _deviceOutputNode;
private AudioFrameInputNode _frameInputNode;
public double Theta;
public DrivePage()
{
InitializeComponent();
Current = this;
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
_rootPage = MainPage.Current;
await CreateAudioGraph();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
_graph?.Dispose();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
}
private unsafe AudioFrame GenerateAudioData(uint samples)
{
// Buffer size is (number of samples) * (size of each sample)
// We choose to generate single channel (mono) audio. For multi-channel, multiply by number of channels
uint bufferSize = samples * sizeof(float);
AudioFrame audioFrame = new AudioFrame(bufferSize);
using (AudioBuffer buffer = audioFrame.LockBuffer(AudioBufferAccessMode.Write))
using (IMemoryBufferReference reference = buffer.CreateReference())
{
// Get the buffer from the AudioFrame
// ReSharper disable once SuspiciousTypeConversion.Global
// ReSharper disable once UnusedVariable
((IMemoryBufferByteAccess) reference).GetBuffer(out var dataInBytes, out var capacityInBytes);
// Cast to float since the data we are generating is float
var dataInFloat = (float*)dataInBytes;
float freq = 1000; // choosing to generate frequency of 1kHz
float amplitude = 0.3f;
int sampleRate = (int)_graph.EncodingProperties.SampleRate;
double sampleIncrement = (freq * (Math.PI * 2)) / sampleRate;
// Generate a 1kHz sine wave and populate the values in the memory buffer
for (int i = 0; i < samples; i++)
{
double sinValue = amplitude * Math.Sin(Theta);
dataInFloat[i] = (float)sinValue;
Theta += sampleIncrement;
}
}
return audioFrame;
}
private void node_QuantumStarted(AudioFrameInputNode sender, FrameInputNodeQuantumStartedEventArgs args)
{
// GenerateAudioData can provide PCM audio data by directly synthesizing it or reading from a file.
// Need to know how many samples are required. In this case, the node is running at the same rate as the rest of the graph
// For minimum latency, only provide the required amount of samples. Extra samples will introduce additional latency.
uint numSamplesNeeded = (uint)args.RequiredSamples;
if (numSamplesNeeded != 0)
{
AudioFrame audioData = GenerateAudioData(numSamplesNeeded);
_frameInputNode.AddFrame(audioData);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (generateButton.Content != null && generateButton.Content.Equals("Generate Audio"))
{
_frameInputNode.Start();
generateButton.Content = "Stop";
audioPipe.Fill = new SolidColorBrush(Colors.Blue);
}
else if (generateButton.Content != null && generateButton.Content.Equals("Stop"))
{
_frameInputNode.Stop();
generateButton.Content = "Generate Audio";
audioPipe.Fill = new SolidColorBrush(Color.FromArgb(255, 49, 49, 49));
}
}
private async Task CreateAudioGraph()
{
// Create an AudioGraph with default settings
AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media);
CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);
if (result.Status != AudioGraphCreationStatus.Success)
{
// Cannot create graph
_rootPage.NotifyUser($"AudioGraph Creation Error because {result.Status.ToString()}", NotifyType.ErrorMessage);
return;
}
_graph = result.Graph;
// Create a device output node
CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await _graph.CreateDeviceOutputNodeAsync();
if (deviceOutputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
{
// Cannot create device output node
_rootPage.NotifyUser(
$"Audio Device Output unavailable because {deviceOutputNodeResult.Status.ToString()}", NotifyType.ErrorMessage);
speakerContainer.Background = new SolidColorBrush(Colors.Red);
}
_deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;
_rootPage.NotifyUser("Device Output Node successfully created", NotifyType.StatusMessage);
speakerContainer.Background = new SolidColorBrush(Colors.Green);
// Create the FrameInputNode at the same format as the graph, except explicitly set mono.
AudioEncodingProperties nodeEncodingProperties = _graph.EncodingProperties;
nodeEncodingProperties.ChannelCount = 1;
_frameInputNode = _graph.CreateFrameInputNode(nodeEncodingProperties);
_frameInputNode.AddOutgoingConnection(_deviceOutputNode);
frameContainer.Background = new SolidColorBrush(Colors.Green);
// Initialize the Frame Input Node in the stopped state
_frameInputNode.Stop();
// Hook up an event handler so we can start generating samples when needed
// This event is triggered when the node is required to provide data
_frameInputNode.QuantumStarted += node_QuantumStarted;
// Start the graph since we will only start/stop the frame input node
_graph.Start();
}
}
GunFireMonitorPage.xaml
<Page
x:Class="SmartPileInspector.xLite.GunFireMonitorPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded"
HorizontalAlignment="Center"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer HorizontalAlignment="Center">
<StackPanel HorizontalAlignment="Center">
<!-- more page content -->
<Grid HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="55"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
<AppBarButton x:Name="generateButton" Content="Generate Audio" Click="Button_Click" MinWidth="120" MinHeight="45" Margin="0,50,0,0"/>
<Border x:Name="frameContainer" BorderThickness="0" Background="#4A4A4A" MinWidth="120" MinHeight="45" Margin="0,20,0,0">
<TextBlock x:Name="frame" Text="Frame Input" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
<StackPanel>
<Rectangle x:Name="audioPipe" Margin="0,20,0,0" Height="10" MinWidth="160" Fill="#313131" HorizontalAlignment="Stretch"/>
</StackPanel>
<Border x:Name="speakerContainer" BorderThickness="0" Background="#4A4A4A" MinWidth="120" MinHeight="45" Margin="0,20,0,0">
<TextBlock x:Name="speaker" Text="Output Device" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
<!--</AppBar>-->
</StackPanel>
</ScrollViewer>
</Page>
There is no graph generated. And there is continuous beep sound with blue line.
Any help is greatly appreciated
Update: Implemented AudioVisualizer
With the help of AudioVisualizer, I was able to plot the lice audio graph.
AudioGraph _graph;
AudioDeviceInputNode _inputNode;
PlaybackSource _source;
SourceConverter _converter;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
_rootPage = MainPage.Current;
_rootPage.SetDimensions(700, 600);
base.OnNavigatedTo(e);
CreateAudioGraphAsync();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
_graph?.Stop();
_graph?.Dispose();
_graph = null;
}
async void CreateAudioGraphAsync()
{
var graphResult = await AudioGraph.CreateAsync(new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.Media));
if (graphResult.Status != AudioGraphCreationStatus.Success)
throw new InvalidOperationException($"Graph creation failed {graphResult.Status}");
_graph = graphResult.Graph;
var inputNodeResult = await _graph.CreateDeviceInputNodeAsync(MediaCategory.Media);
if (inputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
{
_inputNode = inputNodeResult.DeviceInputNode;
_source = PlaybackSource.CreateFromAudioNode(_inputNode);
_converter = new SourceConverter
{
Source = _source.Source,
MinFrequency = 110.0f,
MaxFrequency = 3520.0f,
FrequencyCount = 12 * 5 * 5,
FrequencyScale = ScaleType.Linear,
SpectrumRiseTime = TimeSpan.FromMilliseconds(20),
SpectrumFallTime = TimeSpan.FromMilliseconds(200),
RmsRiseTime = TimeSpan.FromMilliseconds(20),
RmsFallTime = TimeSpan.FromMilliseconds(500),
ChannelCount = 1
};
// Note A2
// Note A7
// 5 octaves, 5 bars per note
// Use RMS to gate noise, fast rise slow fall
NotesSpectrum.Source = _converter;
_graph.Start();
}
else
{
_rootPage.NotifyUser("Cannot access microphone", NotifyType.ErrorMessage);
}
}
Now the challenge is how do I wire an event when wave frequency is above a threshold? In that event I would like to count number of shots, timestamp and it's intensity.
Example Sound
Here is my Recording of live sound, as you can here, when there is that big hammer strike (every second or less), I would like to call a event.
You can find the decibels of a frame by finding the average amplitude of all the pcm data from that frame.I believe you want create a graph that handles the input so that looks like this
private static event LoudNoise<double>;
private static int quantum = 0;
static AudioGraph ingraph;
private static AudioDeviceInputNode deviceInputNode;
private static AudioFrameOutputNode frameOutputNode;
public static async Task<bool> CreateInputDeviceNode(string deviceId)
{
Console.WriteLine("Creating AudioGraphs");
// Create an AudioGraph with default settings
AudioGraphSettings graphsettings = new AudioGraphSettings(AudioRenderCategory.Media);
graphsettings.EncodingProperties = new AudioEncodingProperties();
graphsettings.EncodingProperties.Subtype = "Float";
graphsettings.EncodingProperties.SampleRate = 48000;
graphsettings.EncodingProperties.ChannelCount = 2;
graphsettings.EncodingProperties.BitsPerSample = 32;
graphsettings.EncodingProperties.Bitrate = 3072000;
//settings.DesiredSamplesPerQuantum = 960;
//settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.ClosestToDesired;
CreateAudioGraphResult graphresult = await AudioGraph.CreateAsync(graphsettings);
if (graphresult.Status != AudioGraphCreationStatus.Success)
{
// Cannot create graph
return false;
}
ingraph = graphresult.Graph;AudioGraphSettings nodesettings = new AudioGraphSettings(AudioRenderCategory.GameChat);
nodesettings.EncodingProperties = AudioEncodingProperties.CreatePcm(48000, 2, 32);
nodesettings.DesiredSamplesPerQuantum = 960;
nodesettings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.ClosestToDesired;
frameOutputNode = ingraph.CreateFrameOutputNode(ingraph.EncodingProperties);
quantum = 0;
ingraph.QuantumStarted += Graph_QuantumStarted;
DeviceInformation selectedDevice;
string device = Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId(Windows.Media.Devices.AudioDeviceRole.Default);
if (!string.IsNullOrEmpty(device))
{
selectedDevice = await DeviceInformation.CreateFromIdAsync(device);
} else
{
return false;
}
CreateAudioDeviceInputNodeResult result =
await ingraph.CreateDeviceInputNodeAsync(MediaCategory.Media, nodesettings.EncodingProperties, selectedDevice);
if (result.Status != AudioDeviceNodeCreationStatus.Success)
{
// Cannot create device output node
return false;
}
deviceInputNode = result.DeviceInputNode;
deviceInputNode.AddOutgoingConnection(frameOutputNode);
frameOutputNode.Start();
ingraph.Start();
return true;
}
private static void Graph_QuantumStarted(AudioGraph sender, object args)
{
if (++quantum % 2 == 0)
{
AudioFrame frame = frameOutputNode.GetFrame();
float[] dataInFloats;
using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
using (IMemoryBufferReference reference = buffer.CreateReference())
{
// Get the buffer from the AudioFrame
((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacityInBytes);
float* dataInFloat = (float*)dataInBytes;
dataInFloats = new float[capacityInBytes / sizeof(float)];
for (int i = 0; i < capacityInBytes / sizeof(float); i++)
{
dataInFloats[i] = dataInFloat[i];
}
}
double decibels = 0f;
foreach (var sample in dataInFloats)
{
decibels += Math.Abs(sample);
}
decibels = 20 * Math.Log10(decibels / dataInFloats.Length);
// You can pass the decibel value where ever you'd like from here
if (decibels > 10)
{
LoudNoise?.Invoke(this, decibels);
}
}
}
P.S. I did all of this static but naturally it'll work if it's all in the same instance
I also copied this partially from my own project so it may have some parts I forgot to trim. Hope it helps
Answering the "is this the right approach" question: no, the AudioStateMonitor will not help with the problem.
AudioStateMonitor.SoundLevelChanged tells you if the system is ducking your sound so it doesn't interfere with something else. For example, it may mute music in favour of the telephone ringer. SoundLevelChanged doesn't tell you anything about the volume or frequency of recorded sound, which is what you'll need to detect your handclap.
The right approach will be along the lines of using an AudioGraph (or WASAPI, but not from C#) to capture the raw audio into an AudioFrameOutputNode to process the signal and then run that through an FFT to detect sounds in your target frequencies and volumes. The AudioCreation sample demonstrates using an AudioGraph, but not specifically AudioFrameOutputNode.
Per https://home.howstuffworks.com/clapper1.htm clapping will be in a frequency range of 2200Hz to 2800Hz.
Recognizing gunshots looks like it's significantly more complicated, with different guns having very different signatures. A quick search found several research papers on this rather than trivial algorithms. I suspect you'll want some sort of Machine Learning to classify these. Here's a previous thread discussing using ML to differ between gunshots and non-gunshots: SVM for one Vs all acoustic signal classification

How to unite gameobject meshes?

Because of I made my house from the unity editor I can't remove this mesh borders. I tried MeshCombiner but it just connects all of the meshes in one mesh borders still exist. I know I can make it from Blender or something like this but is there any one to remove from unity?
Screen Shot: http://imgur.com/a/1XALE
Maybe you are looking for CombineMeshes. An example is
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class ExampleClass : MonoBehaviour {
void Start() {
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length) {
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.active = true;
}
}

ExternalAccessory on iOS at Xamarin

Anybody has a clue on how to use the ExternalAccessory API on Xamarin.iOS?
My Xamarin Studio version is 4.0.12(build 3), Xamarin.Android version 4.8.1, Xamarin.iOS version 6.4.5.0 and Xcode is Version 5.0 (5A1413) and I tried target both 6.1 and 7.0 iPad/iPhone.
I've walked the internet and there is not much documentation. Even the MonoTouch docs have broken links.
What I want is, list the connected bluetooth devices, get one of then by name and then connect to it so I can open a socket and start sending data to it. It is a device that uses Serial communication and yes, it has the Apple external accessory protocol ID.
I've tried this:
var am = EAAccessoryManager.SharedAccessoryManager;
It just throws me an exception an InvaidCastException.
Any clues?
Thanks! I really appreciate the help.
PS: Xamarin Details
Xamarin Studio
Version 4.0.12 (build 3)
Installation UUID: 7348d641-ed6d-4c8a-b59a-116674e06dfd
Runtime:
Mono 3.2.0 ((no/7c7fcc7)
GTK 2.24.20
GTK# (2.12.0.0)
Package version: 302000000
[...]
Apple Developer Tools
Xcode 5.0 (3332.25)
Build 5A1413
[...]
Xamarin.iOS
Version: 6.4.5.0 (Trial Edition)
Hash: 1336a36
Branch:
Build date: 2013-10-09 11:14:45-0400
Build Information
Release ID: 400120003
Git revision: 593d7acb1cb78ceeeb482d5133cf1fe514467e39
Build date: 2013-08-07 20:30:53+0000
Xamarin addins: 25a0858b281923e666b09259ad4746b774e0a873
Operating System
Mac OS X 10.8.5
Darwin Gutembergs-MacBook-Pro.local 12.5.0 Darwin Kernel Version 12.5.0
Mon Jul 29 16:33:49 PDT 2013
root:xnu-2050.48.11~1/RELEASE_X86_64 x86_64
Although it seems like you've worked this out, I thought I'd show some code snippets that show the basics (in this case connecting to a Sphero and turning it green):
EAAccessoryManager mgr = EAAccessoryManager.SharedAccessoryManager;
var accessories = mgr.ConnectedAccessories;
foreach(var accessory in accessories)
{
myLabel.Text = "Got me an accessory";
Console.WriteLine(accessory.ToString());
Console.WriteLine(accessory.Name);
var protocol = "com.orbotix.robotprotocol";
if(accessory.ProtocolStrings.Where(s => s == protocol).Any())
{
myLabel.Text = "Got me a Sphero";
var session = new EASession(accessory, protocol);
var outputStream = session.OutputStream;
outputStream.Delegate = new MyOutputStreamDelegate(myLabel);
outputStream.Schedule(NSRunLoop.Current, "kCFRunLoopDefaultMode");
outputStream.Open();
}
}
and
public class MyOutputStreamDelegate : NSStreamDelegate
{
UILabel label;
bool hasWritten = false;
public MyOutputStreamDelegate(UILabel label)
{
this.label = label;
}
public override void HandleEvent(NSStream theStream, NSStreamEvent streamEvent)
{
if(streamEvent == NSStreamEvent.HasSpaceAvailable && ! hasWritten)
{
//Set the color of the Sphero
var written = ((NSOutputStream)theStream).Write(new byte[] {0xFF, 0xFF, 0x02, 0x20, 0x0e, 0x05, 0x1F, 0xFF, 0x1B, 0x00, 0x91}, 11);
if(written == 11)
{
label.Text = "Sphero should be green";
}
hasWritten = true;
}
}
}
I know you specifically asked about writing data to the bluetooth device, but this is just expanding on reading data, as well as the general use of the External Accessory API for Xamarin.iOS because there isn't much documentation or Xamarin samples out there. This is a loose conversion from the Apple sample done with Objective-C. My accessory was a MFi certified microchip reader. I've only put in the "read" functionality since I only needed that for my app.
Create a SessionController class inheriting from NSStreamDelegate and this does a lot of the plumbing. Opens, closes sessions, handles events from the device and reads the data. You'd add your write methods here too I think.
public class EASessionController : NSStreamDelegate
{
NSString SessionDataReceivedNotification = (NSString)"SessionDataReceivedNotification";
public static EAAccessory _accessory;
public static string _protocolString;
EASession _session;
NSMutableData _readData;
public static EASessionController SharedController()
{
EASessionController sessionController = null;
if (sessionController == null)
{
sessionController = new EASessionController();
}
return sessionController;
}
public void SetupController(EAAccessory accessory, string protocolString)
{
_accessory = accessory;
_protocolString = protocolString;
}
public bool OpenSession()
{
Console.WriteLine("opening new session");
_accessory.WeakDelegate = this;
if (_session == null)
_session = new EASession(_accessory, _protocolString);
// Open both input and output streams even if the device only makes use of one of them
_session.InputStream.Delegate = this;
_session.InputStream.Schedule(NSRunLoop.Current, NSRunLoopMode.Default);
_session.InputStream.Open();
_session.OutputStream.Delegate = this;
_session.OutputStream.Schedule(NSRunLoop.Current, NSRunLoopMode.Default);
_session.OutputStream.Open();
return (_session != null);
}
public void CloseSession()
{
_session.InputStream.Unschedule(NSRunLoop.Current, NSRunLoopMode.Default);
_session.InputStream.Delegate = null;
_session.InputStream.Close();
_session.OutputStream.Unschedule(NSRunLoop.Current, NSRunLoopMode.Default);
_session.OutputStream.Delegate = null;
_session.OutputStream.Close();
_session = null;
}
/// <summary>
/// Get Number of bytes to read into local buffer
/// </summary>
/// <returns></returns>
public nuint ReadBytesAvailable()
{
return _readData.Length;
}
/// <summary>
/// High level read method
/// </summary>
/// <param name="bytesToRead"></param>
/// <returns></returns>
public NSData ReadData(nuint bytesToRead)
{
NSData data = null;
if (_readData.Length >= bytesToRead)
{
NSRange range = new NSRange(0, (nint)bytesToRead);
data = _readData.Subdata(range);
_readData.ReplaceBytes(range, IntPtr.Zero, 0);
}
return data;
}
/// <summary>
/// Low level read method - read data while there is data and space in input buffer, then post notification to observer
/// </summary>
void ReadData()
{
nuint bufferSize = 128;
byte[] buffer = new byte[bufferSize];
while (_session.InputStream.HasBytesAvailable())
{
nint bytesRead = _session.InputStream.Read(buffer, bufferSize);
if (_readData == null)
{
_readData = new NSMutableData();
}
_readData.AppendBytes(buffer, 0, bytesRead);
Console.WriteLine(buffer);
}
// We now have our data from the device (stored in _readData), so post the notification for an observer to do something with the data
NSNotificationCenter.DefaultCenter.PostNotificationName(SessionDataReceivedNotification, this);
}
/// <summary>
/// Handle the events occurring with the external accessory
/// </summary>
/// <param name="theStream"></param>
/// <param name="streamEvent"></param>
public override void HandleEvent(NSStream theStream, NSStreamEvent streamEvent)
{
switch (streamEvent)
{
case NSStreamEvent.None:
Console.WriteLine("StreamEventNone");
break;
case NSStreamEvent.HasBytesAvailable:
Console.WriteLine("StreamEventHasBytesAvailable");
ReadData();
break;
case NSStreamEvent.HasSpaceAvailable:
Console.WriteLine("StreamEventHasSpaceAvailable");
// Do write operations to the device here
break;
case NSStreamEvent.OpenCompleted:
Console.WriteLine("StreamEventOpenCompleted");
break;
case NSStreamEvent.ErrorOccurred:
Console.WriteLine("StreamEventErroOccurred");
break;
case NSStreamEvent.EndEncountered:
Console.WriteLine("StreamEventEndEncountered");
break;
default:
Console.WriteLine("Stream present but no event");
break;
}
}
}
In my ViewController that's going to display the data I've just read from the external accessory, we wire it all up. In ViewDidLoad, create observers so the view knows when an event has been fired by the device. Also check we're connected to the correct accessory and open a session.
public EASessionController _EASessionController;
EAAccessory[] _accessoryList;
EAAccessory _selectedAccessory;
NSString SessionDataReceivedNotification = (NSString)"SessionDataReceivedNotification";
string myDeviceProtocol = "com.my-microchip-reader.1234";
public override void ViewDidLoad()
{
base.ViewDidLoad();
NSNotificationCenter.DefaultCenter.AddObserver(EAAccessoryManager.DidConnectNotification, EADidConnect);
NSNotificationCenter.DefaultCenter.AddObserver(EAAccessoryManager.DidDisconnectNotification, EADidDisconnect);
NSNotificationCenter.DefaultCenter.AddObserver(SessionDataReceivedNotification, SessionDataReceived);
EAAccessoryManager.SharedAccessoryManager.RegisterForLocalNotifications();
_EASessionController = EASessionController.SharedController();
_accessoryList = EAAccessoryManager.SharedAccessoryManager.ConnectedAccessories;
foreach (EAAccessory acc in _accessoryList)
{
if (acc.ProtocolStrings.Contains(myDeviceProtocol))
{
// Connected to the correct accessory
_selectedAccessory = acc;
_EASessionController.SetupController(acc, myDeviceProtocol);
_EASessionController.OpenSession();
lblEAConnectionStatus.Text = acc.Name;
Console.WriteLine("Already connected via bluetooth");
}
else
{
// Not connected
}
}
}
Create the DidConnect, DidDisconnect and SessionDataReceived methods. The device name is just updated on some labels when connected/disconnected and I'm displaying the data in text field.
void EADidConnect(NSNotification notification)
{
EAAccessory connectedAccessory = (EAAccessory)notification.UserInfo.ObjectForKey((NSString)"EAAccessoryKey");
Console.WriteLine("I did connect!!");
_accessoryList = EAAccessoryManager.SharedAccessoryManager.ConnectedAccessories;
// Reconnect and open the session in case the device was disconnected
foreach (EAAccessory acc in _accessoryList)
{
if (acc.ProtocolStrings.Contains(myDeviceProtocol))
{
// Connected to the correct accessory
_selectedAccessory = acc;
Console.WriteLine(_selectedAccessory.ProtocolStrings);
_EASessionController.SetupController(acc, myDeviceProtocol);
_EASessionController.OpenSession();
}
else
{
// Not connected
}
}
Console.WriteLine(connectedAccessory.Name);
// Update a label to show it's connected
lblEAConnectionStatus.Text = connectedAccessory.Name;
}
void EADidDisconnect(NSNotification notification)
{
Console.WriteLine("Accessory disconnected");
_EASessionController.CloseSession();
lblEAConnectionStatus.Text = string.Empty;
}
/// <summary>
/// Data receieved from accessory
/// </summary>
/// <param name="notification"></param>
void SessionDataReceived(NSNotification notification)
{
EASessionController sessionController = (EASessionController)notification.Object;
nuint bytesAvailable = 0;
while ((bytesAvailable = sessionController.ReadBytesAvailable()) > 0)
{
// read the data as a string
NSData data = sessionController.ReadData(bytesAvailable);
NSString chipNumber = new NSString(data, NSStringEncoding.UTF8);
// Displaying the data
txtMircochipNumber.Text = chipNumber;
}
}

How can I grayscale a emf image

Using C# and Visual Studio 2010, how can I make a grayscaled emf from a colored one? Should I enumerate records and change color settings somehow?
As a result I want to have a new emf image.
Here is an open source emf read/write utility. I don't think it can be done with GDI+ and preserve the vector content. So this hopefully will help you, but I haven't remotely tested all possible emf cases.
https://wmf.codeplex.com/
this works for both wmf and emf formats. We can combine the Oxage.Wmf strategy with the strategy from the other post which essentially re-colors a pen or brush when it is created so that anything drawn with that pen or brush should be gray.
using Oxage.Wmf;
using Oxage.Wmf.Records;
using Oxage.Wmf.Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MetafileTest
{
public class GrayMap
{
public void GrayFile(string sourceFile, string destFile){
WmfDocument wmf = new WmfDocument();
wmf.Load(sourceFile);
foreach (var record in wmf.Records)
{
if (record is WmfCreateBrushIndirectRecord)
{
var brush = record as WmfCreateBrushIndirectRecord;
brush.Color = Gray(brush.Color);
}
else if (record is WmfCreatePenIndirectRecord)
{
var pen = record as WmfCreatePenIndirectRecord;
pen.Color = Gray(pen.Color);
}
else if (record is WmfCreatePalette) {
var pal = record as WmfCreatePalette;
foreach (PaletteEntry entry in pal.Palette.PaletteEntries) {
Color test = Color.FromArgb(entry.Red, entry.Green, entry.Blue);
Color grayTest = Gray(test);
entry.Red = grayTest.R;
entry.Green = grayTest.G;
entry.Blue = grayTest.B;
}
}
}
wmf.Save(destFile);
}
public Color Gray(Color original) {
int r = (int)(original.R*0.2989);
int g = (int)(original.G*0.5870);
int b = (int)(original.B*0.1140);
Color result = Color.FromArgb(r, g, b);
return result;
}
}
}
This was tested with the following super simple case of filling a blue rectangle, so for at least the simple cases this will work. It may not be able to handle all cases, but in such a case you can probably extend the original source to suite your needs since it is open source.
private void button1_Click(object sender, EventArgs e)
{
var wmf = new WmfDocument();
wmf.Width = 1000;
wmf.Height = 1000;
wmf.Format.Unit = 288;
wmf.AddPolyFillMode(PolyFillMode.WINDING);
wmf.AddCreateBrushIndirect(Color.Blue, BrushStyle.BS_SOLID);
wmf.AddSelectObject(0);
wmf.AddCreatePenIndirect(Color.Black, PenStyle.PS_SOLID, 1);
wmf.AddSelectObject(1);
wmf.AddRectangle(100, 100, 800, 800);
wmf.AddDeleteObject(0);
wmf.AddDeleteObject(1);
wmf.Save("D:\\test.emf");
}
private void button2_Click(object sender, EventArgs e)
{
GrayMap map = new GrayMap();
map.GrayFile("D:\\test.emf", "D:\\test2.emf");
}

Print to a label printer from a web site / web application? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Are there any known label printers that will accept print commands from a web site or web application?
Specifically, the one-off label printers such as Dymo, Brother, Zebra, etc.
Has anyone had success in printing to these printers (without using an Internet Explorer-Only hack or ActiveX control)
I would think that, eventually, there would have to be a solution as we move into a more browser-centric world.
Wow. I know this was asked over 4 years ago, but having burnt the better part of a week in search of a robust method of printing labels from a web app, I had to voice in here.
Here's what I've found:
DYMO seems the most likely candidate for excellence. But no, turns out it only prints from (a) its own app, or (b) something using its SDK. The DYMO SDK uses an XML drawing model that is both overly complex and limited in layout and styling. The documentation is scattered and incomprehensible (e.g. what are the <Bounds> values for the common label sizes? There's no description of the tag parameters anywhere!) So frustrating, so disappointing.
There's qz (was jzebra), which enables browser printing for devices that speak EPL, ZPL, FGL, ESC/POS, EPCL and CPCL ... which includes the Zebra series. It requires a load of integration (running a web server on device the label printer is attached to), but it works.
There's a well designed 3rd party app by Peninsula, which works for DYMO (among others), but requires a middle-step of printing from browser to PDF. They also assume you'll never scale what you want printed down less than 70%, which is incorrect.
The OP says "I would think that, eventually, there would have to be a solution as we move into a more browser-centric world." Four years later, I'd go a step further and suggest any label printer that can't print off a browser (or just behave like a regular printer with small paper) is falling WAY short of its potential. Layout in HTML+CSS is a snap. Browsers parse it perfectly, and render it at any resolution to any output device. It seems so obvious.
If anyone knows of a thermal label printer that prints from the browser instead of imprisoning you in archaic integration methodologies, I'd very much like to know!
The Dymo printers have a browser plugin that let you print from javascript. Very easy to use.
Revisiting this question a few years later.
The networked Zebra printers are easy to print to. In one project I had the webapplication open a socket to the printer and feed it instructions in ZPL.
You could also trying looking PrintNode which provide a cloud printing solution and means you can print straight to any printer over the internet.
A cross-browser/platform-compatible web page/web page script doesn't have the low-level access necessary to transmit printer language command (such as Zebra Printer Language [ZPL]) to the printer. In order to do this, an ActiveX control/browser plugin/applet/similar bit of executable code is required. This limitation is non-printer specific, coming from the browser not the printer manufacturer.
However, many label printers allow you to print to them as though they were normal printers--just ones that print to very small pieces of paper. You could layout your label using HTML/CSS. When displaying the results, instruct the user to go to "File > Print" and select their label printer before clicking print.
Hope this helps,
Ben
Zebra now has a tool called 'BrowserPrint' that works like PrintNode specifically for Zebra printers. (unlike Printnode, it's free to use with Zebra printers)
Some printers also have ppd files available and/or can be configured in cups on a linux platform system or otherwise connected and spoken too via linux. (which then means it's not impossible to use something from shell scripting, php, perl or other methods to print to it by building a simple server daemon or setting up something like apache or lighttpd on a linux box or even a raspberry pi connected to the printer)
You can print from a signed Java applet, or from a Java Web Start application:
http://java.sun.com/javase/technologies/desktop/printing/
That should work, if you format your print output so it fits on the labels.
Note that most modern browsers are restricting support for Java applets, so you may run into trouble with an applet, depending on the browsers in use. In particular, Google Chrome will stop supporting Java applets in September 2015. These restrictions do not apply to Java Web Start, which should continue to work.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Management;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetAllPrinterList();
this.textBox1.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
this.lstPrinterList.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
}
private void GetAllPrinterList()
{
//ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
//objScope.Connect();
//SelectQuery selectQuery = new SelectQuery();
//selectQuery.QueryString = "Select * from win32_Printer";
//ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
//ManagementObjectCollection MOC = MOS.Get();
//foreach (ManagementObject mo in MOC)
//{
// lstPrinterList.Items.Add(mo["Name"].ToString());
// //lstPrinterList.Items.Add(new ListItem(mo["Name"].ToString()));
//}
lstPrinterList.Items.Add("\\\\10.32.65.6\\Parcel-Zebra-FDX ZM400");
lstPrinterList.Items.Add("\\\\10.32.65.4\\Singles_Station_Z_Printer");
lstPrinterList.Items.Add("\\\\10.32.65.12\\ZebraZ4M-Packaging");
if (lstPrinterList.Items.Count > 3)
{
lstPrinterList.Items.RemoveAt(5);
lstPrinterList.Items.RemoveAt(4);
lstPrinterList.Items.RemoveAt(3);
}
//LocalPrintServer printServer = new LocalPrintServer();
//string printer;
//printer = LocalPrintServer.DefaultPrintQueue;
//System.Management.ObjectQuery oquery =
// new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
// System.Management.ManagementObjectSearcher mosearcher =
// new System.Management.ManagementObjectSearcher(oquery);
// System.Management.ManagementObjectCollection moc = mosearcher.Get();
// foreach (ManagementObject mo in moc)
// {
// System.Management.PropertyDataCollection pdc = mo.Properties;
// foreach (System.Management.PropertyData pd in pdc)
// {
// // if ((bool)mo["Network"])
// // {
// lstPrinterList.Items.Add(mo["Name"].ToString());
// // }
// }
// }
//}
// using (var printServer = new PrintServer(string.Format(#"\\{0}", PrinterServerName)))
//{
// foreach (var queue in printServer.GetPrintQueues())
// {
// if (!queue.IsShared)
// {
// continue;
// }
// Debug.WriteLine(queue.Name);
// }
// }
//foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
//{
//MessageBox.Show(printer);
//System.Web.UI.WebControls.ListBox lstPrinterList = new System.Web.UI.WebControls.ListBox();
//System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("lstPrinterList");
//lstPrinterList.Text = "Zebra 110PAX4 (203 ;dpi)";
//lstPrinterList.Items.Add(printer.ToString());
//lstPrinterList.Items.Add(new ListItem(printer));
//lstPrinterList.Items.Add(printer);
//System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("ListBox1");
//Zebra 110PAX4 (203 dpi)
//lstPrinterList.Items.Insert(printer);
//}
}
//private void lstPrinterList_OnClick(object sender, System.EventArgs e)
//{
// // Get the currently selected item in the ListBox.
// string curItem = lstPrinterList.SelectedItem.ToString();
// // Find the string in ListBox2.
// int index = lstPrinterList.DataTextField(curItem);
// //int index = lstPrinterList.FindString(curItem);
// //lstPrinterList.DataTextField(curItem);
// // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
// if (index == -1)
// MessageBox.Show("Item is not available in ListBox2");
// else
// lstPrinterList.SetSelected(index, true);
//}
//private void button1_Click(object sender, System.EventArgs e)
//{
// string str = File.ReadAllText("lpn.prn");
// str = str.Replace("BOX_TYPE", "boom");
// str = str.Replace("11111111", textBox1.Text);
// File.WriteAllText("lpn.prn", str);
// // Print the file to the printer.
// RawPrinterHelper.SendFileToPrinter("\\\\Zebra-FDX ZM400 200 dpi (ZPL)", "C:\\Users\\Administrator\\Documents\\Visual Studio 2015\\Projects\\WindowsFormsApplication4\\Prn Print\\bin\\Debug\\lpn.prn");
//}
public void button1_Click(object sender, System.EventArgs e)
{
String prnFile = "lpn2.prn";
string s = File.ReadAllText(prnFile);
string printer;
String Printer = lstPrinterList.SelectedItem.ToString();
s = s.Replace("*description*", "snuffles");
s = s.Replace("*barcode*", textBox1.Text);
//File.WriteAllText("PPlpn.prn", s);
//s = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
printer = lstPrinterList.SelectedItem.Value;
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
RawPrinterHelper.SendStringToPrinter(printer, s);
//RawPrinterHelper.SendStringToPrinter(Printer, s);
//Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, "?__VIEWSTATE=%2%3D&ctl00%24MainContent%24textBox1=bp300&ctl00%24MainContent%24lstPrinterList=Shipping+Printer"));
}
protected void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
This is what I created in C# and it worked out great. This is a web app.

Resources