<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Загрузка скрина на imgur (BitMap to Base64)]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=13895</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=13895&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Загрузка скрина на imgur (BitMap to Base64)».]]></description>
		<lastBuildDate>Fri, 29 Jun 2018 12:18:15 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK: Загрузка скрина на imgur (BitMap to Base64)]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=126550#p126550</link>
			<description><![CDATA[<div class="quotebox"><cite>powercat пишет:</cite><blockquote><p>примеров найти не смог</p></blockquote></div><p>По слову &quot;imgur&quot; искать пробовали?<br />На форуме <strong>teadrinker</strong> уже выкладывал готовый код.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Fri, 29 Jun 2018 12:18:15 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=126550#p126550</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK: Загрузка скрина на imgur (BitMap to Base64)]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=126548#p126548</link>
			<description><![CDATA[<div class="quotebox"><cite>powercat пишет:</cite><blockquote><p>сразу кодировать файл из bitmap в base64</p></blockquote></div><p>А в каком виде изображение должно появиться на imgur?</p>]]></description>
			<author><![CDATA[null@example.com (teadrinker)]]></author>
			<pubDate>Fri, 29 Jun 2018 11:13:59 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=126548#p126548</guid>
		</item>
		<item>
			<title><![CDATA[AHK: Загрузка скрина на imgur (BitMap to Base64)]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=126546#p126546</link>
			<description><![CDATA[<p>Добрый день. Данный скрипт делает снимок экрана, сохраняет его в .png файл, кодирует файл в base64 и загружает на imgur. На все действия тратится примерно 2-3 секунды + остается файл на диске. Полагаю, что возможно пропустить пункт с преобразованием файла из bitmap в .png и последующим его сохранением. То есть сразу кодировать файл из bitmap в base64 и загружать на imgur. Нужна помощь с реализацией, примеров найти не смог. Спасибо.</p><div class="codebox"><pre><code>
screenshot:=(A_ScriptDir . &quot;\screenshot.png&quot;)
SaveScreenshotToFile(0, 0, A_ScreenWidth, A_ScreenHeight, screenshot)
SendImageToImgur(screenshot,ClientID:=&quot;&quot;)

SaveScreenshotToFile(x, y, w, h, filePath)
{
	hBitmap := GetHBitmapFromScreen(x, y, w, h)
	gdip := new GDIplus
	pBitmap := gdip.BitmapFromHBitmap(hBitmap)
	DllCall(&quot;DeleteObject&quot;, Ptr, hBitmap)
	gdip.SaveBitmapToFile(pBitmap, filePath)
	gdip.DisposeImage(pBitmap)
}

GetHBitmapFromScreen(x, y, w, h)
{
	hDC := DllCall(&quot;GetDC&quot;, Ptr, 0, Ptr)
	hBM := DllCall(&quot;CreateCompatibleBitmap&quot;, Ptr, hDC, Int, w, Int, h, Ptr)
	pDC := DllCall(&quot;CreateCompatibleDC&quot;, Ptr, hDC, Ptr)
	oBM := DllCall(&quot;SelectObject&quot;, Ptr, pDC, Ptr, hBM, Ptr)
	DllCall(&quot;BitBlt&quot;, Ptr, pDC, Int, 0, Int, 0, Int, w, Int, h, Ptr, hDC, Int, x, Int, y, UInt, 0x00CC0020)
	DllCall(&quot;SelectObject&quot;, Ptr, pDC, Ptr, oBM)
	DllCall(&quot;DeleteDC&quot;, Ptr, pDC)
	DllCall(&quot;ReleaseDC&quot;, Ptr, 0, Ptr, hDC)
	Return hBM
}

SendImageToImgur(imagePath, ClientID, JpegQuality := &quot;&quot;)  
{
	oFile := FileOpen(imagePath, &quot;r&quot;)
	oFile.Pos := 0
	oFile.RawRead(buff, size := oFile.length)
	oFile.Close()
	strBase64 := CryptBinaryToStringBASE64(&amp;buff, size, true)
	http := ComObjCreate(&quot;Msxml2.XMLHTTP&quot;)
	http.open(&quot;POST&quot;, &quot;https://api.imgur.com/3/image&quot;, true)
	http.SetRequestHeader(&quot;authorization&quot;, &quot;Client-ID &quot; . ClientID)
	http.send(strBase64)
	VarSetCapacity(strBase64, 0), VarSetCapacity(buff, 0)
	while !(http.readyState = 4)
		Continue
	json:=http.responseText
	RegExMatch(json, &quot;&quot;&quot;link&quot;&quot;:&quot;&quot;(.*?)&quot;&quot;&quot;, match)
	link := StrReplace(match1, &quot;\&quot;)
	return link
}

CryptBinaryToStringBASE64(pData, Bytes, NOCRLF = &quot;&quot;)  {
	static CRYPT_STRING_BASE64 := 1, CRYPT_STRING_NOCRLF := 0x40000000
	CRYPT := CRYPT_STRING_BASE64 | (NOCRLF ? CRYPT_STRING_NOCRLF : 0)
	DllCall(&quot;Crypt32\CryptBinaryToString&quot;, Ptr, pData, UInt, Bytes, UInt, CRYPT, Ptr, 0, UIntP, Chars)
	VarSetCapacity(OutData, Chars * (A_IsUnicode ? 2 : 1))
	DllCall(&quot;Crypt32\CryptBinaryToString&quot;, Ptr, pData, UInt, Bytes, UInt, CRYPT, Str, OutData, UIntP, Chars)
	Return OutData
}

class GDIplus   {
	__New()  {
		if !DllCall(&quot;GetModuleHandle&quot;, Str, &quot;gdiplus&quot;, Ptr)
			DllCall(&quot;LoadLibrary&quot;, Str, &quot;gdiplus&quot;)
		VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
		DllCall(&quot;gdiplus\GdiplusStartup&quot;, PtrP, pToken, Ptr, &amp;si, Ptr, 0)
		this.token := pToken
	}
	__Delete()  {
		DllCall(&quot;gdiplus\GdiplusShutdown&quot;, Ptr, this.token)
		if hModule := DllCall(&quot;GetModuleHandle&quot;, Str, &quot;gdiplus&quot;, Ptr)
			DllCall(&quot;FreeLibrary&quot;, Ptr, hModule)
	}
	BitmapFromHBitmap(hBitmap, Palette := 0)  {
		DllCall(&quot;gdiplus\GdipCreateBitmapFromHBITMAP&quot;, Ptr, hBitmap, Ptr, Palette, PtrP, pBitmap)
		return pBitmap
	}
	SaveBitmapToFile(pBitmap, sOutput, Quality=75)  {
		SplitPath, sOutput,,, Extension
		if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
			return -1
		DllCall(&quot;gdiplus\GdipGetImageEncodersSize&quot;, UIntP, nCount, UIntP, nSize)
		VarSetCapacity(ci, nSize)
		DllCall(&quot;gdiplus\GdipGetImageEncoders&quot;, UInt, nCount, UInt, nSize, Ptr, &amp;ci)
		if !(nCount &amp;&amp; nSize)
			return -2
		Loop, % nCount  {
			sString := StrGet(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), &quot;UTF-16&quot;)
			if !InStr(sString, &quot;*.&quot; Extension)
				continue
			pCodec := &amp;ci+idx
			break
		}
		if !pCodec
			return -3
		if RegExMatch(Extension, &quot;i)^J(PG|PEG|PE|FIF)$&quot;) &amp;&amp; Quality != 75  {
			DllCall(&quot;gdiplus\GdipGetEncoderParameterListSize&quot;, Ptr, pBitmap, Ptr, pCodec, UintP, nSize)
			VarSetCapacity(EncoderParameters, nSize, 0)
			DllCall(&quot;gdiplus\GdipGetEncoderParameterList&quot;, Ptr, pBitmap, Ptr, pCodec, UInt, nSize, Ptr, &amp;EncoderParameters)
			Loop, % NumGet(EncoderParameters, &quot;UInt&quot;)  {
				elem := (24+A_PtrSize)*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
				if (NumGet(EncoderParameters, elem+16, &quot;UInt&quot;) = 1) &amp;&amp; (NumGet(EncoderParameters, elem+20, &quot;UInt&quot;) = 6)  {
					p := elem+&amp;EncoderParameters-pad-4
					NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, &quot;UInt&quot;)), &quot;UInt&quot;)
					break
				}
			}
		}
		if A_IsUnicode
			pOutput := &amp;sOutput
		else  {
			VarSetCapacity(wOutput, StrPut(sOutput, &quot;UTF-16&quot;)*2, 0)
			StrPut(sOutput, &amp;wOutput, &quot;UTF-16&quot;)
			pOutput := &amp;wOutput
		}
		E := DllCall(&quot;gdiplus\GdipSaveImageToFile&quot;, Ptr, pBitmap, Ptr, pOutput, Ptr, pCodec, UInt, p ? p : 0)
		return E ? -5 : 0
	}
	DisposeImage(pBitmap)  {
		return DllCall(&quot;gdiplus\GdipDisposeImage&quot;, Ptr, pBitmap)
	}
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (powercat)]]></author>
			<pubDate>Fri, 29 Jun 2018 09:40:58 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=126546#p126546</guid>
		</item>
	</channel>
</rss>
